# 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. Regular Expression Matching（正则表达式匹配）

• jmspan
• 2016年05月19日 05:10
• 972

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

• buptlrw
• 2016年01月05日 16:07
• 1283

## (java)leetcode-10

• qq_33792412
• 2017年03月29日 22:15
• 244

## LeetCode10. Regular Expression Matching有点难

Implement regular expression matching with support for '.' and '*'. '.' Matches any single characte...
• booirror
• 2016年01月23日 22:02
• 870

## leetcode之通配符

Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Match...
• fangjian1204
• 2014年08月18日 17:55
• 1045

## leetcode10

Combination Sum II #coding=utf-8 class Solution(object): def combinationSum2(self, candidate...
• s969966195
• 2017年07月28日 10:29
• 51

## Leetcode--easy系列10

#205 Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two str...
• u010498696
• 2015年06月25日 21:46
• 509

## LeetCode(10)--RegularExpresssionMatching

• ruosiliu_123
• 2017年07月12日 21:28
• 117

## leetcode10-Regular Expression Matching之Java版本

• peace1213
• 2016年09月12日 12:30
• 855

## leetcode 10 -- Regular Expression Matching

Regular Expression Matching 题目： Implement regular expression matching with support for ‘.’ and ...
• wwh578867817
• 2015年05月28日 20:48
• 874

举报原因： 您举报文章：LeetCode(10)RegularExpresssionMatching 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)