10. Regular Expression Matching

原创 2018年04月17日 16:55:28

Given an input string (s) and a pattern (p), 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).

Note:

  • s could be empty and contains only lowercase letters a-z.
  • p could be empty and contains only lowercase letters a-z, and characters like . or *.

Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".

Example 3:

Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".

Example 4:

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".

Example 5:

Input:
s = "mississippi"
p = "mis*is*p*."
Output: false


方法一:首先考虑递归解法。

如果p为空,那么表示已经匹配完成,如果此时s还未匹配完则一定无法匹配,反之则匹配成功。

如果p不为空,判断p的第二位是否为‘*’,如果是,分两种情况,第一种是忽略‘*’,s不变,p往后跳2格,递归调用函数。第二种是匹配一格,s前进一格,p不变。

如果p的第二位不为‘*’,则可以直接判断当前位是否相同,然后s、p各进一格递归调用。

注意每次都要判断s是否为空,若为空则返回false。

class Solution {
public:
    bool isMatch(string s, string p) {
        if(p.empty())
            return s.empty();
        if(p[1]=='*')
            return isMatch(s,p.substr(2)) || ( !s.empty() && (s[0]==p[0] || p[0] == '.') && isMatch(s.substr(1),p) ); 
        else
            return !s.empty() && (s[0]==p[0] || p[0] == '.') && isMatch(s.substr(1),p.substr(1));
    } 
};

方法二:动态规划。

其实和方法一思想基本相同,只是把递归转换为了dp数组。

class Solution {
public:
    bool isMatch(string s, string p) {
        if(p.empty())
            return s.empty();
        int len1=s.size(),len2=p.size();
        vector<vector<bool>> dp(len1+1,vector<bool>(len2+1,false));
        dp[0][0]=true;
        for(int i=0;i<=len1;i++)
        {
            for(int j=1;j<=len2;j++)
            {
                if(p[j-1]=='*')
                    dp[i][j] = dp[i][j-2] || ( i>0 && dp[i-1][j] && (s[i-1]==p[j-2] || p[j-2]=='.') );
                else
                    dp[i][j] = i>0 && dp[i-1][j-1] && (s[i-1]==p[j-1] || p[j-1]=='.');
            }
        }
        return dp[len1][len2];
    } 
};





---===浅谈位图的淡入淡出和渐隐===---

 浅谈位图的淡入淡出和渐隐成都电子科技大学 朱宁 在许多游戏和屏幕保护程序中,我们都可以发现位图的淡入淡出和渐隐(一幅图象渐渐的消失于另一幅图象中)的应用。如何实现这些效果呢?在windows(GDI...
  • coolstar
  • coolstar
  • 2001-05-04 17:25:00
  • 1115

LeetCode题解--10. Regular Expression Matching

链接LeetCode题目:GitHUb代码:CSDN题解:题意Implement regular expression matching with support for ‘.’ and ‘*’.‘....
  • gatieme
  • gatieme
  • 2016-04-03 11:40:23
  • 3886

10. Regular Expression Matching(很重要!!!)

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character...
  • gao1440156051
  • gao1440156051
  • 2016-08-15 17:15:09
  • 837

leetcode 10: Regular Expression Matching 分析及解答

解读: •‘.'代表一个任意字符,与其附近的字符无关 •’*‘代表o个或者多个前面的字符,该字符影响前面字符的“存在”,例如:a*={ε,a,aa,aaa,…},即当"*"表示0个前面的字符时,该字符...
  • u012150792
  • u012150792
  • 2016-07-22 20:56:43
  • 487

[LeetCode][10]Regular Expression Matching解析 -Java实现

Q: Implement regular expression matching with support for '.' and '*'. '.' Matches any single ...
  • u014629433
  • u014629433
  • 2016-06-11 14:38:15
  • 2030

[LeetCode] 10. Regular Expression Matching java

/**10. Regular Expression Matching * @param s * @param p * @returnboolean 正则匹配,字符串s是否...
  • u012937029
  • u012937029
  • 2017-03-07 09:13:59
  • 91

leetcode 10: Regular Expression Matching

Regular Expression Matching Total Accepted: 39732 Total Submissions: 192279 Implement re...
  • xudli
  • xudli
  • 2015-05-01 02:22:23
  • 2338

[LeetCode]10.Regular Expression Matching

【题目】 mplement regular expression matching with support for '.' and '*'. '.' Matches any single cha...
  • SunnyYoona
  • SunnyYoona
  • 2014-04-03 21:16:40
  • 2931

★leetcode10_Regular Expression Matching[附动态规划]

一.问题描述 Implement regular expression matching with support for '.' and '*'. '.' Matches any single ...
  • u014265088
  • u014265088
  • 2016-09-18 15:21:51
  • 1150

Leetcode: Regular Expression Matching

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character...
  • doc_sgl
  • doc_sgl
  • 2013-10-14 23:09:15
  • 19784
收藏助手
不良信息举报
您举报文章:10. Regular Expression Matching
举报原因:
原因补充:

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