Do not confused with check if (*p == '\0') return *s == '\0';
p is the format that used to match. If p is already reaching end, s still has some chars left, they can not match.
1 class Solution { 2 public: 3 bool isMatch(const char *s, const char *p) { 4 if (*p == '\0') return *s == '\0'; 5 if (*(p+1) != '*') return (*s == *p || *s != '\0' && *p == '.') && isMatch(s+1, p+1); 6 while (*s == *p || *s != '\0' && *p == '.') { 7 if (isMatch(s, p+2)) return true; 8 s++; 9 } 10 return isMatch(s, p+2); 11 } 12 };