Skip to content

Commit de08192

Browse files
committed
leetcode 44
python
1 parent 759e2e8 commit de08192

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Personal Practice Set - Doing One a Day (sometimes) in a Variety of Languages (M
4545
| 39 | Medium | [Combination Sum](leetcode/39-Medium-Combination-Sum/problem.md) | [Python](leetcode/39-Medium-Combination-Sum/answer.py) |
4646
| 40 | Medium | [Combination Sum II](leetcode/40-Medium-Combination-Sum-II/problem.md) | [Python](leetcode/40-Medium-Combination-Sum-II/answer.py) |
4747
| 42 | Hard | [Trapping Rain Water](leetcode/42-Hard-Trapping-Rain-Water/problem.md) | [Python](leetcode/42-Hard-Trapping-Rain-Water/answer.py) |
48+
| 44 | Hard | [Wildcard Matching](leetcode/44-Hard-Wildcard-Matching/problem.md) | [Python](leetcode/44-Hard-Wildcard-Matching/answer.py) |
4849
| 46 | Medium | [Permutations](leetcode/46-Medium-Permutations/problem.md) | [Python](leetcode/46-Medium-Permutations/answer.py) |
4950
| 47 | Medium | [Permutations II](leetcode/47-Medium-Permutations-II/problem.md) | [Python](leetcode/47-Medium-Permutations-II/answer.py) |
5051
| 48 | Medium | [Rotate Image](leetcode/48-Medium-Rotate-Image/problem.md) | [Python](leetcode/48-Medium-Rotate-Image/answer.py) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/python3
2+
3+
#------------------------------------------------------------------------------
4+
# First Solution
5+
# Not too efficient with all the recursion lol
6+
#------------------------------------------------------------------------------
7+
8+
class Solution(object):
9+
def isMatch(self, s, p):
10+
"""
11+
:type s: str
12+
:type p: str
13+
:rtype: bool
14+
"""
15+
i = 0
16+
for j in range(len(p)):
17+
# If str is longer than pattern -> false
18+
if i > len(s)-1:
19+
# it could potentially be true if the remaining characters are *s
20+
if p[j:].replace("*","") == "":
21+
return True
22+
return False
23+
if p[j] == '?':
24+
# If ?, then increment index
25+
i += 1
26+
elif p[j] == '*':
27+
# If last char in pattern, then true
28+
if j == len(p)-1:
29+
return True
30+
# We want to check all possibilities
31+
for curr in range(i, len(s)):
32+
# If the current char is equal to the next char in the pattern or ?
33+
if s[curr] == p[j+1] or p[j+1] == '?' or p[j+1] == '*':
34+
if self.isMatch(s[curr:], p[j+1:]):
35+
return True
36+
return False
37+
else:
38+
# If any char, check char and increment
39+
if p[j] != s[i]:
40+
return False
41+
i += 1
42+
return i == len(s)
43+
44+
45+
46+
#------------------------------------------------------------------------------
47+
# DP Solution
48+
#------------------------------------------------------------------------------
49+
class Solution(object):
50+
def isMatch(self, s, p):
51+
"""
52+
:type s: str
53+
:type p: str
54+
:rtype: bool
55+
"""
56+
ls = len(s)
57+
lp = len(p)
58+
dp = [[False]*(len(p)+1) for i in range(len(s)+1)]
59+
60+
dp[0][0] = True
61+
62+
for i in range(1,len(p)+1):
63+
if p[i-1] == '*' and dp[0][i-1]:
64+
dp[0][i] = True
65+
66+
for row in range(1, len(s)+1):
67+
for col in range(1, len(p)+1):
68+
if s[row-1] == p[col-1] or p[col-1]=='?':
69+
dp[row][col] = True and dp[row-1][col-1]
70+
elif p[col-1] == '*':
71+
dp[row][col] = dp[row][col-1] or dp[row-1][col-1] or dp[row-1][col]
72+
return dp[len(s)][len(p)]
73+
74+
#------------------------------------------------------------------------------
75+
#Testing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Problem 44: Wildcard Matching
2+
3+
#### Difficulty: Hard
4+
5+
#### Problem
6+
7+
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'.
8+
9+
'?' Matches any single character.
10+
11+
'*' Matches any sequence of characters (including the empty sequence).
12+
13+
The matching should cover the entire input string (not partial).
14+
15+
#### Example
16+
17+
<pre>
18+
Example 1:
19+
20+
Input:
21+
s = "aa"
22+
p = "a"
23+
Output: false
24+
Explanation: "a" does not match the entire string "aa".
25+
Example 2:
26+
27+
Input:
28+
s = "aa"
29+
p = "*"
30+
Output: true
31+
Explanation: '*' matches any sequence.
32+
Example 3:
33+
34+
Input:
35+
s = "cb"
36+
p = "?a"
37+
Output: false
38+
Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.
39+
Example 4:
40+
41+
Input:
42+
s = "adceb"
43+
p = "*a*b"
44+
Output: true
45+
Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce".
46+
Example 5:
47+
48+
Input:
49+
s = "acdcb"
50+
p = "a*c?b"
51+
Output: false
52+
</pre>

0 commit comments

Comments
 (0)