Regular Expression Matching
——If you are stuck, recursion is your friend.
Regular Expression Matching
——If you are stuck, recursion is your friend.
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
题解:正则表达式匹配。其中字符‘.’匹配任意一个字符,字符‘*’与前面的一个字符是要捆绑为一体的。如"c*"可表示0个'c',或者1个及以上的个'c'。这样的话,最后一个isMatch("aab", "c*a*b")才能说是true的,因为这里的“c*”可以直接表示为0个'c',直接将其忽略就可以了!
一、思路
想了很久,按情况进行分类处理。发现,‘.’和‘*’的各种组合太多了,根本就罗列不了那么多!看到有句话是这样说的“
If you are stuck, recursion is your friend.
”便开始琢磨递归的思路。
参考:LeetCode – Regular Expression Matching in Java
Regular Expression Matching
http://tech.ddvip.com/2014-08/1407732980212448.html