-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
match-substring-after-replacement.py
50 lines (40 loc) · 1.4 KB
/
match-substring-after-replacement.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
# Time: O(n * k), n = len(s), k = len(sub)
# Space: O(m), m = len(mappings)
import collections
# brute force
class Solution(object):
def matchReplacement(self, s, sub, mappings):
"""
:type s: str
:type sub: str
:type mappings: List[List[str]]
:rtype: bool
"""
def transform(x):
return ord(x)-ord('0') if x.isdigit() else ord(x)-ord('a')+10 if x.islower() else ord(x)-ord('A')+36
def check(i):
return all(sub[j] == s[i+j] or lookup[sub[j]][s[i+j]] for j in xrange(len(sub)))
lookup = [[0]*62 for _ in xrange(62)]
for a, b in mappings:
lookup[transform(a)][transform(b)] = 1
s = map(transform, s)
sub = map(transform, sub)
return any(check(i) for i in xrange(len(s)-len(sub)+1))
# Time: O(n * k), n = len(s), k = len(sub)
# Space: O(m), m = len(mappings)
import collections
# brute force
class Solution2(object):
def matchReplacement(self, s, sub, mappings):
"""
:type s: str
:type sub: str
:type mappings: List[List[str]]
:rtype: bool
"""
def check(i):
return all(sub[j] == s[i+j] or (sub[j], s[i+j]) in lookup for j in xrange(len(sub)))
lookup = set()
for a, b in mappings:
lookup.add((a, b))
return any(check(i) for i in xrange(len(s)-len(sub)+1))