# 【leetcode】 Regular Expression Matching

Implement regular expression matching with support for '.' and '*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true


aaaaa,a*a
aa,a*aa

1.如果字符串s和p,当s[i]==p[j]或者p[j]==.且p[j+1]!='*',我们就比较s[i+1]和p[j+1]

2.如果p[j+1]为*就需要额外考虑。

（a）如果s[i]!=p[j],且p[j+1]=*，则去比较s[i]和p[j+2]

(b) 如果s[i]==p[j]，则返回 s[i+1]与p[j]  || s[i]与p[j+2] 的情况

 public boolean isMatch(String s, String p) {
System.out.println("s "+s+" p "+p);
if(p==null || p.length()==0)
return s.length()==0;
if(p.length()>1){
if(p.charAt(1)=='*'){
if(s==null||s.length()==0)
return isMatch(s, p.substring(2));
return isMatch(s, p.substring(2))||isMatch(s.substring(1),p );
}else{
if(s==null||s.length()==0)
return false;
if(s.charAt(0)==p.charAt(0) || p.charAt(0)=='.')
return isMatch(s.substring(1), p.substring(1));
}
}else if(p.length()==1){
if(p.charAt(0)=='.'){
if(s==null||s.length()<=1)
return true;
else
return false;
}else{
if(s==null||s.length()==0||s.length()>1)
return false;
return s.charAt(0)==p.charAt(0);
}
}
return false;
}

public boolean isMatch(String s, String p) {

if(p.length() == 0)
return s.length() == 0;

if(p.length() == 1 || p.charAt(1) != '*'){
if(s.length() < 1 || (p.charAt(0) != '.' && s.charAt(0) != p.charAt(0)))
return false;
return isMatch(s.substring(1), p.substring(1));

}else{
int len = s.length();

int i = -1;
while(i<len && (i < 0 || p.charAt(0) == '.' || p.charAt(0) == s.charAt(i))){
if(isMatch(s.substring(i+1), p.substring(2)))
return true;
i++;
}
return false;
}
}