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
https://oj.leetcode.com/problems/regular-expression-matching/
思路1:递归。根据下一个字符是否是'*'分情况判断。
- 如果p的下一个字符不是'*',只需判断当前字符是否相等,或者p[cur]='.',递归处理s[1]和p[1];
- 如果是p的下一个'*',则当前s和p相等或者p='.'情况下,依次判断s[0...s.length]和p2]是否match;
思路2:自动机?有待研究。
递归代码:
public class Solution {
public boolean isMatch(String s, String p) {
if (s == null)
return p == null;
if (p == null)
return s == null;
int lenS = s.length();
int lenP = p.length();
if (lenP == 0)
return lenS == 0;
if (lenP == 1) {
if (p.equals(s) || p.equals(".") && s.length() == 1) {
return true;
} else
return false;
}
if (p.charAt(1) != '*') {
if (s.length() > 0
&& (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.')) {
return isMatch(s.substring(1), p.substring(1));
}
return false;
} else {
while (s.length() > 0
&& (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.')) {
if (isMatch(s, p.substring(2)))
return true;
s = s.substring(1);
}
return isMatch(s, p.substring(2));
}
}
public static void main(String[] args) {
System.out.println(new Solution().isMatch("aa", "a"));
System.out.println(new Solution().isMatch("aa", "aa"));
System.out.println(new Solution().isMatch("aaa", "aa"));
System.out.println(new Solution().isMatch("aa", "a*"));
System.out.println(new Solution().isMatch("aa", ".*"));
System.out.println(new Solution().isMatch("ab", ".*"));
System.out.println(new Solution().isMatch("aab", "c*a*b"));
System.out.println(new Solution().isMatch("", ""));
System.out.println(new Solution().isMatch("abcdeff", ".*"));
System.out.println(new Solution().isMatch("a", "ab*"));
System.out.println(new Solution().isMatch("bb", ".bab"));
}
}
参考:
http://www.programcreek.com/2012/12/leetcode-regular-expression-matching-in-java/
http://fisherlei.blogspot.com/2013/01/leetcode-wildcard-matching.html
http://leetcodenotes.wordpress.com/2013/08/25/leetcode-regular-expression-matching/
来源:oschina
链接:https://my.oschina.net/u/1584603/blog/283584