Description
![]()
Solution
class Solution {
public boolean isMatch(String s, String p) {
// after scan the pattern, if string still has characters, return false
if (p.isEmpty())
return s.isEmpty();
// check if the first letter of string matches the pattern
boolean first = !s.isEmpty() && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.');
// check if the follow letter is *
if (p.length() >= 2 && p.charAt(1) == '*') {
return isMatch(s, p.substring(2)) || (first && isMatch(s.substring(1), p));
} else {
return first && isMatch(s.substring(1), p.substring(1));
}
}
}