-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1202_medium_[disjoint_set]_smallest_string_with_swaps.py
More file actions
66 lines (48 loc) · 1.73 KB
/
1202_medium_[disjoint_set]_smallest_string_with_swaps.py
File metadata and controls
66 lines (48 loc) · 1.73 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
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
class DisjointSet():
def __init__(self, n):
self.forest = [ i for i in range(n) ]
self.treeNodeNums = [ 1 for i in range(n) ]
def union(self, a, b):
rootA = self.root(a)
rootB = self.root(b)
if rootA == rootB: return
if self.treeNodeNums[a] >= self.treeNodeNums[b]:
self.forest[rootB] = rootA
self.treeNodeNums[rootA] += self.treeNodeNums[rootB]
else:
self.forest[rootA] = rootB
self.treeNodeNums[rootB] += self.treeNodeNums[rootA]
def root(self, a):
initA = a
while self.forest[a] != a:
a = self.forest[a]
self.forest[initA] = a
return a
class Solution():
def smallestStringWithSwaps(self, s, pairs):
dSet = self._createDisjointSet(len(s), pairs)
groups = {}
for i in range(len(dSet.forest)):
groupNum = dSet.root(i)
if groups.get(groupNum) is None:
groups[groupNum] = { "indexList": [i], "charList": [s[i]] }
else:
groups[groupNum]["indexList"].append(i)
groups[groupNum]["charList"].append(s[i])
swapedStringList = [ None for _ in range(len(s)) ]
for groupNum in groups.keys():
indexList = groups[groupNum]["indexList"]
sortedCharList = sorted(groups[groupNum]["charList"])
for i, index in enumerate(indexList):
swapedStringList[index] = sortedCharList[i]
swapedString = "".join(swapedStringList)
return swapedString
def _createDisjointSet(self, n, pairs):
dSet = DisjointSet(n)
for pair in pairs:
a, b = pair
dSet.union(a,b)
return dSet
print(Solution().smallestStringWithSwaps("dcab", [[0,3],[1,2],[0,2]]))
print(Solution().smallestStringWithSwaps("cba", [[0,1],[1,2]]))
print(Solution().smallestStringWithSwaps("ba", [[0, 1], [1, 0]]))