1
+ // Runtime: 51 ms (Top 84.03%) | Memory: 27.8 MB (Top 21.38%)
1
2
class Solution {
2
3
public:
3
- bool match (int i, int j, string &a, string &b, vector<vector<int >>&dp)
4
- {
5
- if (i<0 && j<0 ) return true ;
6
- if (i>=0 && j<0 ) return false ;
7
- if (i<0 && j>=0 )
8
- {
9
- for (;j>-1 ;j--) if (b[j]!=' *' ) return false ;
10
- return true ;
11
- }
12
- if (dp[i][j]!=-1 ) return dp[i][j];
13
- if (a[i]==b[j] || b[j]==' ?' ) return dp[i][j] = match (i-1 ,j-1 ,a,b,dp);
14
- if (b[j]==' *' ) return dp[i][j] = (match (i-1 ,j,a,b,dp) | match (i,j-1 ,a,b,dp));
15
- return false ;
16
- }
17
- bool isMatch (string s, string p) {
18
- int n=s.size (), m=p.size ();
19
- vector<vector<int >>dp (n+1 ,vector<int >(m+1 ,-1 ));
20
- return match (n-1 ,m-1 ,s,p,dp);
21
- }
4
+ bool match (int i, int j, string &a, string &b, vector<vector<int >>&dp)
5
+ {
6
+ if (i<0 && j<0 ) return true ;
7
+ if (i>=0 && j<0 ) return false ;
8
+ if (i<0 && j>=0 )
9
+ {
10
+ for (;j>-1 ;j--) if (b[j]!=' *' ) return false ;
11
+ return true ;
12
+ }
13
+ if (dp[i][j]!=-1 ) return dp[i][j];
14
+ if (a[i]==b[j] || b[j]==' ?' ) return dp[i][j] = match (i-1 ,j-1 ,a,b,dp);
15
+ if (b[j]==' *' ) return dp[i][j] = (match (i-1 ,j,a,b,dp) | match (i,j-1 ,a,b,dp));
16
+ return false ;
17
+ }
18
+ bool isMatch (string s, string p) {
19
+ int n=s.size (), m=p.size ();
20
+ vector<vector<int >>dp (n+1 ,vector<int >(m+1 ,-1 ));
21
+ return match (n-1 ,m-1 ,s,p,dp);
22
+ }
22
23
};
0 commit comments