-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathapply-substitutions.py
93 lines (84 loc) · 2.68 KB
/
apply-substitutions.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Time: O(r * 2^r)
# Space: O(r * 2^r)
import collections
# topological sort
class Solution(object):
def applySubstitutions(self, replacements, text):
"""
:type replacements: List[List[str]]
:type text: str
:rtype: str
"""
def find_adj(s):
result = set()
i = 0
while i < len(s):
if s[i] != '%':
i += 1
continue
j = next(j for j in xrange(i+1, len(s)) if s[j] == '%')
result.add(s[i+1:j])
i = j+1
return result
def replace(s):
result = []
i = 0
while i < len(s):
if s[i] != '%':
result.append(s[i])
i += 1
continue
j = next(j for j in xrange(i+1, len(s)) if s[j] == '%')
result.append(lookup[s[i+1:j]])
i = j+1
return "".join(result)
def topological_sort():
adj = collections.defaultdict(set)
in_degree = collections.defaultdict(int)
for u, s in replacements:
for v in find_adj(s):
adj[v].add(u)
in_degree[u] += 1
result = []
q = [u for u, _ in replacements if not in_degree[u]]
while q:
new_q = []
for u in q:
lookup[u] = replace(lookup[u])
for v in adj[u]:
in_degree[v] -= 1
if in_degree[v]:
continue
new_q.append(v)
q = new_q
return result
lookup = {k:v for k, v in replacements}
topological_sort()
return replace(text)
# Time: O(r * 2^r)
# Space: O(r * 2^r)
# memoization
class Solution2(object):
def applySubstitutions(self, replacements, text):
"""
:type replacements: List[List[str]]
:type text: str
:rtype: str
"""
lookup = {k:v for k, v in replacements}
memo = {}
def replace(s):
if s not in memo:
result = []
i = 0
while i < len(s):
if s[i] != '%':
result.append(s[i])
i += 1
continue
j = next(j for j in xrange(i+1, len(s)) if s[j] == '%')
result.append(replace(lookup[s[i+1:j]]))
i = j+1
memo[s] = "".join(result)
return memo[s]
return replace(text)