[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
class Solution {
public:
    bool isMatch(const char *s, const char *p) {
        if (*p == ‘\0‘) return *s == ‘\0‘;  //empty

        if (*(p + 1) != ‘*‘) {//without *
            if(!matchFirst(s,p))
                return false;
            return isMatch(s + 1, p + 1);
        } else { //next: with a *
            if(isMatch(s, p + 2))
                return true;    //try the length of 0
            while ( matchFirst(s,p) )       //try all possible lengths
                if (isMatch(++s, p + 2))
                    return true;
        }
    }
private:
    bool matchFirst(const char *s, const char *p){
        return (*p == *s || (*p == ‘.‘ && *s != ‘\0‘));
    }
};    
时间: 2024-10-12 12:55:50

[LeetCode] Regular Expression Matching(递归)的相关文章

[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

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 shoul

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

LeetCode Regular Expression Matching 网上一个不错的实现(非递归)

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

[LeetCode] Regular Expression Matching

1. 只要求匹配带'.'和'*' 2. '.'可以当作任何字符来匹配 3. 只有'*'需要特殊处理,'*'可以匹配0个或者多个前面的字符. 注意处理好边界情况. class Solution { public: bool isMatch(string s, string p) { int ans = 0; int n = s.size(); int m = p.size(); vector<vector<int>> visit(n+1, vector<int>(m+1)

LeetCode: Regular Expression Matching 解题报告

Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. SOLUTION 1: 思路: 从后往前遍历罗马数字,如果某个数比前一个数小,则把该数在结果中减掉:反之,则在结果中加上当前这个数: GITHUB 代码: https://github.com/yuzhangcmu/LeetCode_algorithm

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover

[LeetCode][Python]Regular Expression Matching

# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character.'*' Matches zero or more of the preceding element. The matchin

LeetCode (10): Regular Expression Matching [HARD]

https://leetcode.com/problems/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 ent