public class Solution {
public boolean isMatch(String s, String p) {
if(p.length() == 0) {
return s.length() == 0;
}
if(p.length() == 1) {
return s.length() == 1 && (p.charAt(0) == '.' || s.equals(p));
}
// p.length() > 1
if(p.charAt(1) != '*') {
return s.length() > 0 && (p.charAt(0) == '.' || s.charAt(0) == p.charAt(0))
&& isMatch(s.substring(1, s.length()), p.substring(1, p.length()));
} else {
int i = 0;
for( ; i < s.length() && (p.charAt(0) == '.' || s.charAt(i) == p.charAt(0)); i++) {
if(isMatch(s.substring(i, s.length()), p.substring(2, p.length()))) {
return true;
}
}
return isMatch(s.substring(i, s.length()), p.substring(2, p.length()));
}
}
}
Time: O(n^2)
Space: O(n^2)