10. Regular Expression Matching

Posted sherylwang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了10. Regular Expression Matching相关的知识,希望对你有一定的参考价值。

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

一道老题,然而想透却不太容易。首先问能不能匹配,给了两个序列,明显的是当前是否匹配和前部分是否匹配和当前字母有关联,是一道双序列DP题。该题的难点在于‘*’的处理。尤其是‘.*’的处理。这种题目如果在面试当中,一定要多问几个case彻底理解题意。比如Example3 Input: s = "ab", p = ".*", Output: true。可以看出‘.*’的组合,可以生成任意长度的字符去匹配s。而‘**’不能匹配任何s字符。

设置dp[i][j]对应前s的前i-1个字符是否匹配p的前j-1个字符

所以在求解过程中。针对当前字符是否为“*”做处理。转换状态为:

1. dp[i+1][j+1] = dp[i][j]  if p[j] != ‘*‘ and (p[j] == ‘.‘ or s[i] == p[j])   (p[j]不是‘*’的情况)

2.dp[i+1][j+1] = dp[i+1][j-1] if p[j] == ‘*‘ and j > 0 (p[j]是‘*’,前面字符重复0次)

3.dp[i+1][j+1] = dp[i][j+1] if j > 0 and p[j-1] == ‘.‘ or s[i] == p[j-1] )(p[j]是‘*’,前面字符重复至少一次,既然前面字符重复至少一次,则把s[:i]去掉一个字符也可以和p[:j]匹配。

class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        #把s, p为空的单独判断融合到主逻辑里面。之前是把s不为空,p为空
        m = len(s)
        n = len(p)
        dp = [[False] * (n+1) for i in xrange(m+1)]
        
        dp[0][0] = True
        
        for i in xrange(n): # s="", p != ""
            if p[i] == * and (i > 0 and p[i-1] != *) and dp[0][i-1] == True:
                dp[0][i+1] = True
         
        for i in xrange(m):
            for j in xrange(n):
                if p[j] != *:
                    if p[j] == . or s[i] == p[j]:
                        dp[i+1][j+1] = dp[i][j]
                else: 
                    if j > 0 and dp[i+1][j-1] == True: # *匹配0个字符
                        dp[i+1][j+1] = True
                    else:                              # *匹配1个以上字符
                        if j > 0 and p[j-1] == . or s[i] == p[j-1]:
                            dp[i+1][j+1] = dp[i][j+1]         
        return dp[m][n]        
        

 

以上是关于10. Regular Expression Matching的主要内容,如果未能解决你的问题,请参考以下文章

10. Regular Expression Matching

10. Regular Expression Matching

10. Regular Expression Matching

10. Regular Expression Matching

10 Regular Expression Matching

10. Regular Expression Matching