【 Regular Expression Matching 】cpp

题目:

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(string s, string p) {
        // both s and p reach their ends
        if ( p.empty() ) return s.empty();
        // p‘s next is not *
        if ( p[1]!=‘*‘ )
        {
            return ( s[0]==p[0] || (p[0]==‘.‘ && !s.empty()) ) && Solution::isMatch(s.substr(1), p.substr(1));
        }
        // p‘s next is * and curr s match curr p
        int i = 0;
        for ( ; s[i]==p[0] || (p[0]==‘.‘ && i<s.size()); ++i)
        {
            if ( Solution::isMatch(s.substr(i), p.substr(2)) ) return true;
        }
        // p‘s next is * but curr s not match curr p
        return Solution::isMatch(s.substr(i), p.substr(2));
    }
};

tips:

这个代码在Leetcode最新接口上,是可以AC的。

思路有些复杂:主要判断下一个p是否是*,并分类讨论。

虽然代码AC了,但是有些疑惑的地方依然存在:

1. p[1]!=‘*‘ 这个语句是会遇到p[1]不存在(索引越界)的情况的,在我的mac上返回的结果是NUL,但是OJ可以通过。

2. p.substr(2)这个语句也会遇到与1同样的问题。

虽然OJ通过了,但是还有各种边界隐患。如果string索引越界了,应该是引发未定义行为,但是不知道为何能通过。

改用DP做一遍,看一下边界条件是否可以简化些。

时间: 2024-10-13 12:21:51

【 Regular Expression Matching 】cpp的相关文章

【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 (hard) ★

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】10.Regular Expression Matching

题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. 解题思路: 这道题如果只考虑“.”的话其实很好完成,所以解题的关键在于处理“*”的情况.以为“*”与前一个字母有关,所以应该整体考虑ch*……的情况.ch*可以匹配0-n个s的字符串

【leetcode】Regular Expression Matching

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

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 010 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 shou

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

LeetCode第[10]题(Java):Regular Expression Matching

题目:匹配正则表达式 题目难度:hard 题目内容: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 fu

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