蠡口44. Wildcard Matching

Two strings, the comparison returns T or F, the title like 10 Regular Expression Matching. When using dynamic programming as just a little shift in the state's tricky.

1, to establish a matrix dp: dp [I] [J]: IF s [0: i] The matches P [0: J], note string s [0: i] does not include s [i], so the matrix size dp it should be (lens + 1) * (lenp + 1), the initial value of the whole is F;

2, initialize the boundary conditions: dp [0] [0] indicates "" (null character) and "" (null character) whether the match, of course, is True. dp [i] [0] ( i> 0) must be False, because a null character pattern p impossible to match a non-blank character s. However, a non-null pattern may match a null character, because the pattern of "*" may match the null character, so when dp [0] [j-1 ] (p in front j-1 characters match the null character) and dp [j-1] is "*" dp when [0] [j] is True; if

3, state transition equation: When considering dp [i] [j], p [j-1] may take the following four values:

  1) p [j-1] == s [i-1], then this time only when dp [i-1] [j-1] is T, dp [i] [j] it may be T. Because at that time even if dp [i] [j-1] is T, p [j-1] is not "*", can not match a null character; even if dp [i-1] [j] is T, we determine when the boundary mentioned, s is any one of the characters does not match a null character;

  2) p [j-1] == "?", Because "?" S match any one character, only when the "?" Matches the s [i-1] When, dp [i] [j] only there may be T. Therefore, this case corresponds to the case 1);

  3) p [j-1] is not s [i-1],, any "*" in a one, then dp [i] [j] certainly is F, do not do anything "?";

  4) p [j-1] == "*", at this time if there DP [K] [-J. 1] (K I) is T, then dp [i] [j] is also T. Because the "*" matches s [(k + 1): i]. Use a loop to determine is not wrong, but time out. I started is to do so, the rogue can only ask the great God who online. Then find a tricky: if p [j] == "*" and dp [i-1] [j] == True <=> there is ak (<i), such that dp [k] [j-1] == True! To understand more intuitive, I put a simplified diagram to represent DP [K] [-J. 1],  DP [. 1-I] [J] and DP [I] [J] :

 

 

   At first I do not believe this evil, we work together to prove it:

  <=) If dp [k] [j-1 ] is then T s [0: k] matches p [0: (j-1 )]; plus s [k: (i-1 )] may be "*" (p [j- 1]) match, the s [0: (i-1 )] may be p [0: j] matches, i.e. DP [. 1-I] [J] = T;

  =>) Contradiction, it is assumed for all K (K <I), DP [K] [-J. 1] are both F. No matter p [j-1] The "*" with which s [x: (i-1 )] match, because dp [x] [j-1 ] to F., DP [I-. 1] [J] are F, contradiction!

4, the return value: DP [Lens] [lenp]: S IF [0: Lens] The matches P [0: lenp]

class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        #dp[]
        if len(p)==0: return(len(s)==0)
        lens,lenp=len(s),len(p)
        #dp[i][j]: if s[0:i] matches p[0:j]
        dp=[[False for i in range(lenp+1)] for j in range(lens+1)]
        dp[0][0]=True
        for j in range(1,lenp+1):
            if p[j-1]=="*" and dp[0][j-1]: dp[0][j]=True
        #print(dp)
        for i in range(1,lens+1):
            for j in range(1,lenp+1):
                #print(dp[i][j])
                if p[j-1]==s[i-1] or p[j-1]=="?": dp[i][j]=dp[i-1][j-1]
                elif p[j-1]=="*": dp[i][j]=dp[i-1][j] or dp[i][j-1]
                #Note that if p[j]=="*" and dp[i-1][j]==True   <=>    there is a k(<i), such that dp[k][j-1]==True
        #print(dp)
        return(dp[lens][lenp])

 

Guess you like

Origin www.cnblogs.com/Leisgo/p/11703419.html