# LeetCode(10)RegularExpresssionMatching

'.' 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

isMatch("ab", ".*") → true

//
//  Solution.h
//  LeetCodeOJ_010_RegMatching
//
//  Created by feliciafay on 12/4/13.
//

#ifndef LeetCodeOJ_010_RegMatching_Solution_h
#define LeetCodeOJ_010_RegMatching_Solution_h
#include <iostream>
#include <string>
#include <assert.h>
// Input:	"bbbba", ".*a*a"
class Solution {
public:
bool isMatch(const char *s, const char *p) {
assert(s && p);
if (*p == '\0') return *s == '\0';

// next char is not '*': must match current character
if (*(p+1) != '*') {
assert(*p != '*');
return ((*p == *s) || (*p == '.' && *s != '\0')) && isMatch(s+1, p+1);
}
// next char is '*'
while ((*p == *s) || (*p == '.' && *s != '\0')) {
if (isMatch(s, p+2))
return true;
s++;
}
//继续匹配剩下的部分
return isMatch(s, p+2);
}
};
#endif

(1)欣赏一下这句的简洁和巧妙
return ((*p == *s) || (*p == '.' && *s != '\0')) && isMatch(s+1, p+1);
(2)

if (isMatch(s, p+2)) return true;

return  (isMatch(s, p+2));

(3)

//错误写法
s++;
if (isMatch(s, p+2)) return true;

if (isMatch(s, p+2)) return true;
s++;

if (isMatch(s, p+2)) return true;
s++;
(4) LeetCode对思路讲解的的原话

We need some kind of backtracking mechanism such that when a matching fails, we return to the last successful matching state and attempt to match more characters in s with ‘*’. This approach leads naturally to recursion.

If the next character of p is NOT ‘*’, then it must match the current character of s. Continue pattern matching with the next character of both s and p.
If the next character of p is ‘*’, then we do a brute force exhaustive matching of 0, 1, or more repeats of current character of p… Until we could not match any more characters.

#### LeetCode(10)--RegularExpresssionMatching

2017-07-12 21:28:55

#### (java)leetcode-10

2017-03-29 22:15:59

#### 刷LeetCode刷LeetCode刷LeetCode刷LeetCode

2018年01月09日 1.55MB 下载

#### leetcode第10题——***Regular Expression Matching

2016-01-05 16:07:08

#### ★leetcode10_Regular Expression Matching[附动态规划]

2016-09-18 15:21:51

#### Leetcode 10. 正则表达式匹配

2018-03-03 18:06:37

#### leetcode solution

2018年06月07日 1.15MB 下载

#### leetcode 题解代码整理 6-10题

2015-08-05 15:11:09

#### leetcode-10-正则表达式

2017-05-14 16:42:34

#### leetcode 10 Regular Expression Matching（简单正则表达式匹配）

2017-02-21 01:19:33