简繁EN

LeetCodeNo. 009

[LeetCode with Python] 44. Wildcard Matching

Contents

Problem

Problem page: https://leetcode.com/problems/wildcard-matching/
Topics: Dynamic Programming, Backtracking, Greedy, String
Difficulty: Hard
Similar problems: (H) Regular Expression Matching

Implement wildcard pattern matching with support for '?' and '*'.

'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).

The matching should cover the **entire** input string (not partial).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false

Analysis

The special test cases in this DP problem gave me far more trouble than the complexity of the algorithm itself. Before this, when I optimized DP algorithms, I focused on saving space. It didn't occur to me that I could spot some cases early or preprocess them to cut the running time. Without that, I'd keep getting Time Limit Exceeded on some really long, nasty cases.

  1. During preprocessing, I can replace each run of consecutive * in the pattern p with a single *.
  2. If the total number of question marks and ordinary characters in the pattern is greater than the length of the string s, the match fails.
  3. If s is empty, the match succeeds if the pattern is empty or contains only *. Otherwise, the match fails.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution:
# @param s, an input string
# @param p, a pattern string
# @return a boolean
def isMatch(self, s, p):
if (None == s or '' == s) and (None == p or '' == p):
return True
len_s = len(s)
len_p = len(p)
new_p = ''
last_ch = None
p_solid_char_num = 0
for i in range(0, len_p):
if '*' != last_ch or '*' != p[i]:
new_p += p[i]
last_ch = p[i]
if '*' != p[i]:
p_solid_char_num += 1
if p_solid_char_num > len_s:
return False
isAllStars = True
if (None == s or '' == s) and 0 == p_solid_char_num:
return True
p = new_p
len_p = len(new_p)

F = [[False for j in range(0, len_p + 1)] for i in range(0, 2)]

# 3 cases for True:
# last ch matched, and this ch matcheds (right-down)
# p comes *, and last matched (right)
# last p.ch is *, s comes any ch, and last matched (down)
for i in range(1, len_s + 1):
if 1 == i:
F[0][0] = True
for j in range(1, len_p + 1):
F[0][j] = (F[0][j - 1] and '*' == p[j - 1])
else:
for j in range(0, len_p + 1): # cannot be F[0] = F[1]
F[0][j] = F[1][j]
F[1][0] = False
for j in range(1, len_p + 1):
if True == F[0][j - 1] and (p[j - 1] == s[i - 1] or '?' == p[j - 1] or '*' == p[j - 1]):
F[1][j] = True
elif (True == F[1][j - 1] or True == F[0][j]) and '*' == p[j - 1]:
F[1][j] = True
else:
F[1][j] = False

return F[1][len_p]

Article link: https://xnerv.wang/en/leetcode-wildcard-matching/