zoukankan      html  css  js  c++  java
  • 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]        
            
  • 相关阅读:
    K项目小记
    与职场中年女性聊天,一定要小心!
    不是自己的东西不要拿,是做人最起码的道理
    为什么很多人排斥中国女生嫁去外国?
    北大清华几十位硕士博士挤入街道办事处任职,我的几点看法
    面对一直在房价洼地的长沙,我不后悔十几年前逃离长沙
    SAP QM 源检验(Source Inspection)功能展示
    电视剧《猎毒人》观后感
    特朗普如能连任美国总统,于中国不是坏事
    python-day4-字符串,列表相关方法
  • 原文地址:https://www.cnblogs.com/sherylwang/p/9695223.html
Copyright © 2011-2022 走看看