Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created September 30, 2018 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/71e36aed86e68820d3a40a8b61229753 to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/71e36aed86e68820d3a40a8b61229753 to your computer and use it in GitHub Desktop.
class Solution {
public:
bool isMatch(string s, string p) {
int len_s = s.size(), len_p = p.size();
bool dp[len_s+1][len_p+1];
//Since the dp[len_s][len_p] means comparison of two empty string, which should be true.
dp[len_s][len_p] = true;
for(int i = len_s; i>= 0; i--){
for(int j = len_p; j>= 0; j--){
//We already set the value to true above;
if(i == len_s && j == len_p) continue;
bool firstMatch = (i<len_s && j< len_p && (s[i] == p[j] || p[j] == '.'));
if(j+1 < len_p && p[j+1] == '*'){
dp[i][j] = dp[i][j+2]|| (firstMatch&&dp[i+1][j]);
}
else
dp[i][j] = firstMatch&&dp[i+1][j+1];
}
}
return dp[0][0];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment