-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWildcard Matching
More file actions
62 lines (53 loc) · 2.03 KB
/
Wildcard Matching
File metadata and controls
62 lines (53 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Implement wildcard pattern matching with support for '?' and '*'.
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).
The matching should cover the entire input string (not partial).
The function prototype should be:
bool isMatch(const char *s, const char *p)
Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false
/** dp solution **/
/**
* public bool IsMatch(string str, string pattern){
//dp[i, j] means matching status between str.Substring(0, j) and pattern.Substring(0, i)
bool[,] dp = new bool[pattern.Length + 1, str.Length + 1];
dp[0, 0] = true;
int i = 0, j = 0;
//dp[i, 0] means to match empty string; '*' is matching empty string
while(i < pattern.Length && pattern[i++] == '*') dp[i, 0] = true;
for(i = 1; i <= pattern.Length; i++)
for(j = 1; j <= str.Length; j++)
if (pattern[i - 1] == '*')
dp[i, j] = dp[i - 1, j] || dp[i, j - 1];
else if(pattern[i - 1] == '?' || pattern[i - 1] == str[j - 1])
dp[i, j] = dp[i - 1, j - 1];
else dp[i, j] = false;
return dp[pattern.Length, str.Length];
}*/
class Solution {
public:
bool isMatch(string s, string p) {
//dp[i, j] means matching status between str.Substring(0, j) and pattern.Substring(0, i)
int n = s.size();
int m = p.size();
vector<vector<bool>> dp(m+1,vector<bool>(n+1));
dp[0][0] = true;
int i = 0, j = 0;
//dp[i, 0] means to match empty string; '*' is matching empty string
while(i < m && p[i++] == '*') dp[i][0] = true;
for(i = 1; i <= m; i++)
for(j = 1; j <= n; j++)
if (p[i - 1] == '*')
dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
else if(p[i - 1] == '?' || p[i - 1] == s[j - 1])
dp[i][j] = dp[i - 1][j - 1];
else dp[i][j] = false;
return dp[m][n];
}
};