Leetcode-Regular Expression Matching

原创 2015年07月07日 13:04:54

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
DP

bool isMatch(string s, string p) {
    int sl=s.length();
    int pl=p.length();

    vector<vector<bool>> dp(pl+1,vector<bool>(sl+1,false));
    dp[0][0]=true;

    for(int i=1;i<=pl;++i)
    {
        dp[i][0]=i-2>=0&&p[i-1]=='*'&&dp[i-2][0];
        for(int j=1;j<=sl;++j)
        {
            if(p[i-1]==s[j-1]||p[i-1]=='.')
            {
                dp[i][j]=dp[i-1][j-1];
            }
            else if(p[i-1]=='*'&&i-2>=0)
            {
                if(dp[i-2][j]||((p[i-2]==s[j-1]||p[i-2]=='.')&&dp[i][j-1]))
                {
                    dp[i][j]=true;
                }
                else 
                {
                    dp[i][j]=false;
                }
            }
            else
            {
                dp[i][j]=false;
            }

        }

    }
    return dp[pl][sl];


    }
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode 10.Regular Expression Matching(正则表达式匹配) 解题思路和方法

Regular Expression Matching Implement regular expression matching with support for '.' and '*'. ...

LeetCode之10 --- Regular Expression Matching

题目:   , Implement regular expression matching with support for '.' and '*'. '.' Matches any sin...

leetcode 10. Regular Expression Matching

leetcode 10. Regular Expression Matching 只有两种特殊字符*和. 动态规划DPempty text与empty pattern是match的用状态表T[i +...

[LeetCode] Regular Expression Matching 解题报告

[题目] mplement regular expression matching with support for ‘.’ and ‘*’. ‘.’ Matches any single ch...

leetcode -- Regular Expression Matching -- dp 重点

https://leetcode.com/problems/regular-expression-matching/trick: 如果有两个str list, 考虑用2D dp[i][j]思路1 递归...

LeetCode Regular Expression Matching 极品代码赏析

这个条件很容易忽略,也是最重要的条件了,因为这个相当于*可以把它的前缀字母”吃掉“ 所以也是为什么我们需要“往前看”,看到pattern字符串后一位,假设这个字符串为const char *p; 那...

Regular Expression Matching -- LeetCode

原题链接: http://oj.leetcode.com/problems/regular-expression-matching/  这个题目比较常见,但是难度还是比较大的。我们先来看看brute ...

leetcode 10 Regular Expression Matching

Regular Expression Matching Total Accepted: 51566 Total Submissions: 249926 Implement regul...

LeetCode Regular Expression Matching

Description: Implement regular expression matching with support for '.' and '*'. '.' Matches any s...

[Leetcode]10. Regular Expression Matching @python

题目Implement regular expression matching with support for ‘.’ and ‘*’.‘.’ Matches any single characte...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)