-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complexity Time complexity: O(n^2) Space complexity: O(n)
- Loading branch information
1 parent
dbd4685
commit bd2e1ea
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*' where: | ||
'?' Matches any single character. | ||
'*' Matches any sequence of characters (including the empty sequence). | ||
The matching should cover the entire input string (not partial). | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
bool isMatch(string s, string p) { | ||
int r = p.size(); | ||
int c = s.size(); | ||
vector<bool>arr(c+1,false); | ||
vector<bool>brr(c+1,false); | ||
arr[0] = true; | ||
bool dn = true; | ||
|
||
for(int i=1;i<=r;i++){ | ||
if(p[i-1]!='*') | ||
dn = false; | ||
if(dn) | ||
brr[0] = true; | ||
else | ||
brr[0] = false; | ||
|
||
for(int j=1;j<=c;j++){ | ||
if(p[i-1]=='?'){ | ||
brr[j] = arr[j-1];continue; | ||
} | ||
|
||
if(p[i-1]=='*'){ | ||
brr[j] = arr[j-1] | arr[j] | brr[j-1]; | ||
continue; | ||
} | ||
|
||
if(p[i-1]==s[j-1]) | ||
brr[j] = arr[j-1]; | ||
else | ||
brr[j] = false; | ||
|
||
} | ||
|
||
arr = brr; | ||
} | ||
return arr[c]; | ||
} | ||
}; |