【leetcode刷题笔记】010. Regular Expression Matching

日期:20180913
题目描述:

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 = "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 2:

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

这道题就是正则表达式匹配'.'和'*'。一开始没有什么思路,想到递归,可是又捋不清该怎么归,忍不住看了答案,答案有递归和动态规划两种做法。其中动态规划无论是时间复杂度还是空间复杂度都比递归小。又是一道让我感慨“这也能动态规划”的问题。

动态方程是这么列的:

  • s的第i个元素和p的第j个元素直接相等(包括'.')

    f[i][j] = f[i-1][j-1]

  • p的第j个元素是‘*’,有可能间接相等

    • p的第j-1个元素和s的第i个元素相等

      f[i][j] = f[i][j-2]||f[i][j-1]||f[i-1][j](*之前的元素被匹配了0次或1次或者多次)

    • p的第j-1个元素和s的第i个元素不相等

      f[i][j] = f[i][j-2](*之前的元素被匹配了0次)

  • p的第j个元素不是‘*’,也不直接相等,也就是不可能匹配

    f[i][j] = false

代码如下:

bool isMatch(string s, string p) {
        int slen = s.size();
        int plen = p.size();
        bool f[slen+1][plen+1];
        //把边界设置好
        for(int i = 0;i<=slen;i++){
            f[i][0] = false;
        }
        f[0][0] = true;
        for(int j = 1;j<=plen;j++){
            if(p[j-1]=='*'){
                f[0][j] = f[0][j-2];
            }else{
                f[0][j] = false;
            }
        }
        //开始循环
        for(int i = 1;i<=slen;i++){
            for(int j = 1;j<=plen;j++){
                if(s[i-1]==p[j-1]||p[j-1]=='.'){//s的最后一个和p的最后一个是直接匹配的 
                    f[i][j] = f[i-1][j-1];
                }else if(p[j-1]=='*'){//p的最后是* ,有间接匹配的可能 
                    if(p[j-2]!=s[i-1]&&p[j-2]!='.'){
                        f[i][j] = f[i][j-2];
                    }else{
                        f[i][j] = f[i][j-2]||f[i][j-1]||f[i-1][j];
                    }
                }else{//最后一位不匹配,也没有间接匹配的可能 
                    f[i][j] = false;
                }
            }
        }
        return f[slen][plen];
}
总结:

有时能用递归的很有可能就能用动态规划来做,重点在于列出动态方程,很多时候动态方程是分很多情况的,要把情况想清楚,不重复不遗漏。

你可能感兴趣的:(【leetcode刷题笔记】010. Regular Expression Matching)