-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path44_WildcardMatching.py
executable file
·57 lines (48 loc) · 1.39 KB
/
44_WildcardMatching.py
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
51
52
53
54
55
56
57
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
def isMatch(self, s, p):
""" Dynamic Programming
dp[i][j] represents isMatch(p[0...i-1], s[0...j-1]), default is False;
dp[i][0]: isMatch(p[0...i], ""), dp[0][j]: isMatch("", s[0...j])
dp[0][0] represents
If p[i] is "*", dp[i+1][j+1] =
1. dp[i][j+1] # * matches 0 element in s;
2. dp[i][j] # * matches 1 element in s;
3. dp[i+1][j] # * matches more than one in s.
"""
if not s:
if p.count('*') != len(p):
return False
return True
# Optimized for the big data.
if len(p) - p.count('*') > len(s):
return False
# Initinal process
dp = [[False for col in range(len(s) + 1)] for row in range(len(p) + 1)]
dp[0][0] = True # isMatch("", "") = True
for i in range(len(p)):
dp[i + 1][0] = dp[i][0] and p[i] == '*'
for i in range(len(p)):
for j in range(len(s)):
if p[i] == "*":
dp[i + 1][j + 1] = dp[i][j + 1] or dp[i][j] or dp[i + 1][j]
else:
dp[i + 1][j + 1] = dp[i][j] and (p[i] == s[j] or p[i] == "?")
return dp[len(p)][len(s)]
"""
"aa"
"a"
"aa"
"aa"
"aaa"
"aa"
"aa"
"*"
"aa"
"a*"
"ab"
"?*"
"aab"
"c*a*b"
"""