DEV Community

Cover image for Day 41 of 100 days dsa coding challenge
Manasi Patil
Manasi Patil

Posted on

Day 41 of 100 days dsa coding challenge

Taking on a new challenge: solving GeeksforGeeks POTD daily and sharing my solutions! πŸ’»πŸ”₯
The goal: sharpen problem-solving skills, level up coding, and learn something new every day. Follow my journey! πŸš€

100DaysOfCode #CodingChallenge #ProblemSolving #GeeksforGeeks #DeveloperJourney

Problem:

https://www.geeksforgeeks.org/problems/wildcard-pattern-matching/1

Wildcard Pattern Matching

Difficulty: Medium Accuracy: 31.13%

Given two strings pat and txt which may be of different sizes, You have to return true if the wildcard pattern i.e. pat, matches with txt else return false.
The wildcard pattern pat can include the characters '?' and ''.
β€’ '?' – matches any single character.
β€’ '
' – matches any sequence of characters (including the empty sequence).
Note: The matching should cover the entire txt (not partial txt).

Examples:
Input: txt = "abcde", pat = "a?c*"
Output: true
Explanation: '?' matches with 'b' and '' matches with "de".
Input: txt = "baaabab", pat = "a*ab"
Output: false
Explanation: The pattern starts with a, but the text starts with b, so the pattern does not match the text.
Input: txt = "abc", pat = "
"
Output: true
Explanation: '*' matches with whole text "abc".
Constraints:
1 ≀ txt.size(), pat.size() ≀ 100

Solution:
class Solution:
def wildCard(self, txt, pat):
n, m = len(txt), len(pat)
dp = [[False] * (m + 1) for _ in range(n + 1)]
dp[0][0] = True
for j in range(1, m + 1):
if pat[j - 1] == '':
dp[0][j] = dp[0][j - 1]
for i in range(1, n + 1):
for j in range(1, m + 1):
if pat[j - 1] == txt[i - 1] or pat[j - 1] == '?':
dp[i][j] = dp[i - 1][j - 1]
elif pat[j - 1] == '
':
dp[i][j] = dp[i - 1][j] or dp[i][j - 1]
return dp[n][m]

Top comments (0)