-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0291-word-pattern-ii.py
More file actions
41 lines (32 loc) · 1.23 KB
/
0291-word-pattern-ii.py
File metadata and controls
41 lines (32 loc) · 1.23 KB
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
# time complexity: O(p*n^3)
# space complexity: O(p + n)
from typing import Dict
class Solution:
def wordPatternMatch(self, pattern: str, s: str) -> bool:
def backtrack(pattern: str, s: str, lookup: Dict[str, str]) -> bool:
if pattern == "" or s == "":
return pattern == "" and s == "" and len(set(lookup.values())) == len(set(lookup))
firstPattern = pattern[0]
if firstPattern in lookup:
if s.startswith(lookup[firstPattern]):
return backtrack(pattern[1:], s[len(lookup[firstPattern]):], lookup)
else:
return False
n = len(s)
for i in range(1, n + 1):
lookup[firstPattern] = s[:i]
result = backtrack(pattern[1:], s[i:], lookup)
if result:
return True
del lookup[firstPattern]
return False
return backtrack(pattern, s, {})
pattern = "abab"
s = "redblueredblue"
print(Solution().wordPatternMatch(pattern, s))
pattern = "aaaa"
s = "asdasdasdasd"
print(Solution().wordPatternMatch(pattern, s))
pattern = "aabb"
s = "xyzabcxzyabc"
print(Solution().wordPatternMatch(pattern, s))