|
1 | | -###44. Wildcard Matching |
| 1 | +# 44. Wildcard Matching |
2 | 2 |
|
3 | | -题目: |
4 | | -<https://leetcode.com/problems/wildcard-matching/> |
| 3 | +**<font color=red>难度: Hard</font>** |
5 | 4 |
|
| 5 | +## 刷题内容 |
6 | 6 |
|
7 | | -难度: |
| 7 | +> 原题连接 |
8 | 8 |
|
9 | | -Hard |
| 9 | +* https://leetcode.com/problems/wildcard-matching/description/ |
10 | 10 |
|
| 11 | +> 内容描述 |
11 | 12 |
|
| 13 | +``` |
| 14 | +Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. |
| 15 | +
|
| 16 | +'?' Matches any single character. |
| 17 | +'*' Matches any sequence of characters (including the empty sequence). |
| 18 | +The matching should cover the entire input string (not partial). |
| 19 | +
|
| 20 | +Note: |
| 21 | +
|
| 22 | +s could be empty and contains only lowercase letters a-z. |
| 23 | +p could be empty and contains only lowercase letters a-z, and characters like ? or *. |
| 24 | +Example 1: |
12 | 25 |
|
13 | | -做完Regular Expression Matching来做的这道题,按照DP思路run一下是超时,感觉是开心的,至少暂时没有报错了,有待优化,应该在dp的同时在贪心一下么。 |
| 26 | +Input: |
| 27 | +s = "aa" |
| 28 | +p = "a" |
| 29 | +Output: false |
| 30 | +Explanation: "a" does not match the entire string "aa". |
| 31 | +Example 2: |
14 | 32 |
|
| 33 | +Input: |
| 34 | +s = "aa" |
| 35 | +p = "*" |
| 36 | +Output: true |
| 37 | +Explanation: '*' matches any sequence. |
| 38 | +Example 3: |
15 | 39 |
|
| 40 | +Input: |
| 41 | +s = "cb" |
| 42 | +p = "?a" |
| 43 | +Output: false |
| 44 | +Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'. |
| 45 | +Example 4: |
16 | 46 |
|
17 | | -超时代码 |
| 47 | +Input: |
| 48 | +s = "adceb" |
| 49 | +p = "*a*b" |
| 50 | +Output: true |
| 51 | +Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce". |
| 52 | +Example 5: |
| 53 | +
|
| 54 | +Input: |
| 55 | +s = "acdcb" |
| 56 | +p = "a*c?b" |
| 57 | +Output: false |
18 | 58 | ``` |
| 59 | + |
| 60 | +## 解题方案 |
| 61 | + |
| 62 | +> 思路 1 |
| 63 | +******- 时间复杂度: O(len(s) * len(p))******- 空间复杂度: O(len(s) * len(p))****** |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +dp, 看完下面的代码就知道思路了 |
| 68 | + |
| 69 | +beats 61.92% |
| 70 | + |
| 71 | +```python |
19 | 72 | class Solution(object): |
20 | 73 | def isMatch(self, s, p): |
21 | 74 | """ |
22 | 75 | :type s: str |
23 | 76 | :type p: str |
24 | 77 | :rtype: bool |
25 | 78 | """ |
26 | | - m, n = len(s), len(p) |
27 | | - dp = [ [0 for i in xrange(n+1)] for j in xrange(m+1)] |
| 79 | + t = [[False] * (len(s) + 1) for i in range(len(p) + 1)] |
| 80 | + t[0][0] = True |
| 81 | + for i in range(1, len(p) + 1): |
| 82 | + t[i][0] = t[i-1][0] and p[i-1] == '*' |
| 83 | + for i in range(1, len(p)+1): |
| 84 | + for j in range(1, len(s) + 1): |
| 85 | + if p[i-1] != '*': |
| 86 | + t[i][j] = (p[i-1] == s[j-1] or p[i-1] == '?') and t[i-1][j-1] |
| 87 | + else: |
| 88 | + t[i][j] = t[i][j-1] or t[i-1][j] |
| 89 | + return t[-1][-1] |
| 90 | +``` |
| 91 | + |
| 92 | + |
| 93 | +> 思路 2 |
| 94 | +******- 时间复杂度: O(len(s) * len(p))******- 空间复杂度: O(1)****** |
| 95 | + |
| 96 | +双指针 |
| 97 | + |
| 98 | +``` |
| 99 | +Worst case Should be O(NM) |
| 100 | +think that s ="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" to match p ="*aaaaaab"( '*' in the beginning) |
| 101 | +It's easy to see 'match' is moving step by step to almost the end, each time we move 'match', we will go through the whole tail of p (after '*') until we found out 'b' is not a match. Thus it's O(NM) |
| 102 | +``` |
| 103 | + |
| 104 | +beats 94.80% |
| 105 | + |
| 106 | +```python |
| 107 | +class Solution(object): |
| 108 | + def isMatch(self, s, p): |
| 109 | + """ |
| 110 | + :type s: str |
| 111 | + :type p: str |
| 112 | + :rtype: bool |
| 113 | + """ |
| 114 | + ps, pp, match, star_idx = 0, 0, 0, -1 |
| 115 | + while ps < len(s): |
| 116 | + if pp < len(p) and (p[pp] == '?' or s[ps] == p[pp]): # advancing both pointers |
| 117 | + ps += 1 |
| 118 | + pp += 1 |
| 119 | + elif pp < len(p) and p[pp] == '*': # found '*', only advancing pattern pointer |
| 120 | + star_idx = pp |
| 121 | + match = ps |
| 122 | + pp += 1 |
| 123 | + elif star_idx != -1: # last pattern pointer was *, advancing string pointer |
| 124 | + pp = star_idx + 1 |
| 125 | + match += 1 |
| 126 | + ps = match |
| 127 | + else: # current pattern pointer is not star, last patter pointer was not *, characters do not match |
| 128 | + return False |
| 129 | + while pp < len(p) and p[pp] == '*': # check for remaining characters in pattern |
| 130 | + pp += 1 |
| 131 | + return pp == len(p) |
| 132 | +``` |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
28 | 162 |
|
29 | | - dp[0][0] = 1 |
30 | 163 |
|
31 | | - # init the first line |
32 | | - for i in xrange(1,n+1): |
33 | | - if p[i-1] == '*': |
34 | | - dp[0][i] = dp[0][i-1] |
35 | 164 |
|
36 | | - for i in xrange(1,m+1): |
37 | | - for j in xrange(1,n+1): |
38 | | - if p[j-1] == s[i-1] or p[j-1] == '?': |
39 | | - dp[i][j] = dp[i-1][j-1] |
40 | | - elif p[j-1] == '*': |
41 | | - dp[i][j] = dp[i][j-1] or dp[i-1][j] |
42 | 165 |
|
43 | | - return dp[m][n] == 1 |
44 | | -``` |
|
0 commit comments