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

题目要求实现一个可以支持‘.‘和‘*‘的正则表示,代码如下:

依题意,我们根据字符串P的下一个字符是否是‘*‘来分开讨论(见代码第7行)。如果是,那么必须在两字符串当前位置进行比较;否则,让字符串P的当前位置与字符串S的当前及后面各位比较直至不匹配,并开始下一轮的比较。为了避免字符串P的‘*‘匹配过多的项,还要对S从当前位置开始的子串和P从当前位置后面两位开始的子串进行匹配(样例:a  ab* )。

时间: 2024-11-03 02:10:51

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 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 网上一个不错的实现(非递归)

'.' 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 [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题解||Regular Expression Matching 问题

problem: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the<span style="color:#ff0000;"> preceding element</span>. The matching should cover the entir

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