Wildcard Matching
LeetCode 44 · ಕಠಿಣ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್ಥ್ರೂ
'*' ⇒ dp(i, j+1) or dp(i+1, j).
ನಾನು Wildcard Matching (LeetCode 44) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — String DP / Word Break ಪ್ಯಾಟರ್ನ್ನ ಒಂದು ಕಠಿಣ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.
ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್ನಲ್ಲಿದೆ.
Intuition
Match the string and pattern from the front. A normal character or '?' must consume exactly one character from each. A '*' has two options: match nothing (skip the star) or swallow one more character of s and stay on the star.
Approach
- 1
Pattern finished: match only if the string is finished too.
- 2
String finished: the rest of the pattern must be all '*'.
- 3
Same char or '?': advance both.
- 4
'*': skip it, or consume one character of s and keep the star.
- 5
Tabulation fills from the ends backwards; the last row handles trailing stars.
ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ · ಮೊದಲ ಆಲೋಚನೆಯಿಂದ ಅಂತಿಮ ಪರಿಹಾರದವರೆಗೆ
dfs(index1, index2) with a dict.
dp[i][j] filled bottom-right to top-left.
Recursion + memoPythonO(m·n) time · O(m·n) space
class Solution:
def isMatch(self, s: str, p: str) -> bool:
#case 1
#either char match or we have ?
#case 2
#1) * doesn't match any character - bb, *bb -> move to next pattern
#2) * matches the sequence -aa * ->consume chars of s as much as we can
#base case
#if string is empty check all remaining chars of p are all *
#if pattern is empty check s is empty
m = len(s)
n = len(p)
memo = {}
def dfs(index1,index2):
#base case
key = (index1,index2)
if key in memo:
return memo[key]
if index2==n:
return index1==m
if index1==m:
return all(x=="*" for x in p[index2:])
#main cases
if s[index1]==p[index2] or p[index2]=="?":
memo[key]=dfs(index1+1,index2+1)
elif p[index2]=="*":
memo[key]= dfs(index1,index2+1) or dfs(index1+1,index2)
else:
memo[key]= False
return memo[key]
return dfs(0,0)Tabulation (from the end)PythonO(m·n) time · O(m·n) space
class Solution:
def isMatch(self, s: str, p: str) -> bool:
m = len(s)
n = len(p)
dp=[[False]*(n+1) for _ in range(m+1)]
dp[m][n]=True
for index2 in range(n-1,-1,-1):
if p[index2]=="*":
dp[m][index2]=dp[m][index2+1]
else:
break
for index1 in range(m-1,-1,-1):
for index2 in range(n-1,-1,-1):
if s[index1]==p[index2] or p[index2]=="?":
dp[index1][index2]=dp[index1+1][index2+1]
elif p[index2]=="*":
dp[index1][index2]= dp[index1][index2+1] or dp[index1+1][index2]
else:
dp[index1][index2]= False
return dp[0][0]Wildcard Matching — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು
Wildcard Matching ಹಿಂದಿನ intuition ಏನು?
Match the string and pattern from the front. A normal character or '?' must consume exactly one character from each. A '*' has two options: match nothing (skip the star) or swallow one more character of s and stay on the star.
Wildcard Matching (LeetCode 44) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?
1. Pattern finished: match only if the string is finished too. 2. String finished: the rest of the pattern must be all '*'. 3. Same char or '?': advance both. 4. '*': skip it, or consume one character of s and keep the star. 5. Tabulation fills from the ends backwards; the last row handles trailing stars.
Wildcard Matching ನ time ಮತ್ತು space complexity ಎಷ್ಟು?
Time: O(m · n). Space: O(m · n). (Accepted approach: Tabulation (from the end).)
Wildcard Matching ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?
The 'string empty' base case must allow a run of trailing stars. This is not regex — '*' here matches any sequence on its own.
Wildcard Matching ಅನ್ನು ಪರಿಹರಿಸಲು ಒಂದಕ್ಕಿಂತ ಹೆಚ್ಚು ಮಾರ್ಗಗಳಿವೆಯೇ?
Recursion + memo (O(m·n) time · O(m·n) space): dfs(index1, index2) with a dict. Tabulation (from the end) (O(m·n) time · O(m·n) space): dp[i][j] filled bottom-right to top-left.