forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
smallest-string-with-swaps.py
78 lines (68 loc) · 2.15 KB
/
smallest-string-with-swaps.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
# Time: O(nlogn)
# Space: O(n)
import collections
class UnionFind(object):
def __init__(self, n):
self.set = range(n)
def find_set(self, x):
if self.set[x] != x:
self.set[x] = self.find_set(self.set[x]) # path compression.
return self.set[x]
def union_set(self, x, y):
x_root, y_root = map(self.find_set, (x, y))
if x_root == y_root:
return False
self.set[max(x_root, y_root)] = min(x_root, y_root)
return True
class Solution(object):
def smallestStringWithSwaps(self, s, pairs):
"""
:type s: str
:type pairs: List[List[int]]
:rtype: str
"""
union_find = UnionFind(len(s))
for x,y in pairs:
union_find.union_set(x, y)
components = collections.defaultdict(list)
for i in xrange(len(s)):
components[union_find.find_set(i)].append(s[i])
for i in components.iterkeys():
components[i].sort(reverse=True)
result = []
for i in xrange(len(s)):
result.append(components[union_find.find_set(i)].pop())
return "".join(result)
# Time: O(nlogn)
# Space: O(n)
import itertools
class Solution2(object):
def smallestStringWithSwaps(self, s, pairs):
"""
:type s: str
:type pairs: List[List[int]]
:rtype: str
"""
def dfs(i, adj, lookup, component):
lookup.add(i)
component.append(i)
for j in adj[i]:
if j in lookup:
continue
dfs(j, adj, lookup, component)
adj = collections.defaultdict(list)
for i, j in pairs:
adj[i].append(j)
adj[j].append(i)
lookup = set()
result = list(s)
for i in xrange(len(s)):
if i in lookup:
continue
component = []
dfs(i, adj, lookup, component)
component.sort()
chars = sorted(result[k] for k in component)
for comp, char in itertools.izip(component, chars):
result[comp] = char
return "".join(result)