Created
August 7, 2014 05:50
-
-
Save Onaiplee/8d4762918b3c7835c7c3 to your computer and use it in GitHub Desktop.
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
class Solution { | |
public: | |
bool isMatch(const char *s, const char *p) { | |
int s_size = strlen(s); | |
int p_size = strlen(p); | |
vector<vector<int>> dict(s_size + 1, vector<int>(p_size + 1, -1)); | |
is_match(s, p, 0, 0, dict); | |
return dict[0][0] ? true : false; | |
} | |
int is_match(const char *s, const char *p, int si, int pi, vector<vector<int>> &dict) { | |
if (si < dict.size() - 1 && pi < dict[0].size() - 1) { | |
if (dict[si][pi] != -1) { | |
return dict[si][pi]; | |
} | |
if (*(p+pi+1) == '*') { | |
if (*(s+si) == *(p+pi) || *(p+pi) == '.') { | |
dict[si][pi] = is_match(s, p, si+1, pi, dict) || is_match(s, p, si+1, pi+2, dict) || is_match(s, p, si, pi+2, dict); | |
return dict[si][pi]; | |
} else { | |
dict[si][pi] = is_match(s, p, si, pi+2, dict); | |
return dict[si][pi]; | |
} | |
} else { | |
if (*(s+si) == *(p+pi) || *(p+pi) == '.') { | |
dict[si][pi] = is_match(s, p, si+1, pi+1, dict); | |
return dict[si][pi]; | |
} else { | |
dict[si][pi] = 0; | |
return 0; | |
} | |
} | |
} else if (pi < dict[0].size() - 1) { | |
if (*(p+pi+1) == '*') { | |
dict[si][pi] = is_match(s, p, si, pi+2, dict); | |
return dict[si][pi]; | |
} else { | |
dict[si][pi] = 0; | |
return 0; | |
} | |
} else if (si < dict.size() - 1){ | |
dict[si][pi] = 0; | |
return 0; | |
} else { | |
dict[si][pi] = 1; | |
return 1; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment