1202. Smallest String With Swaps
Description
You are given a string s
, and an array of pairs of indices in the string pairs
where pairs[i] = [a, b]
indicates 2 indices(0-indexed) of the string.
You can swap the characters at any pair of indices in the given pairs
any number of times.
Return the lexicographically smallest string that s
can be changed to after using the swaps.
Example 1:
Input: s = "dcab", pairs = [[0,3],[1,2]] Output: "bacd" Explaination: Swap s[0] and s[3], s = "bcad" Swap s[1] and s[2], s = "bacd"
Example 2:
Input: s = "dcab", pairs = [[0,3],[1,2],[0,2]] Output: "abcd" Explaination: Swap s[0] and s[3], s = "bcad" Swap s[0] and s[2], s = "acbd" Swap s[1] and s[2], s = "abcd"
Example 3:
Input: s = "cba", pairs = [[0,1],[1,2]] Output: "abc" Explaination: Swap s[0] and s[1], s = "bca" Swap s[1] and s[2], s = "bac" Swap s[0] and s[1], s = "abc"
Constraints:
1 <= s.length <= 10^5
0 <= pairs.length <= 10^5
0 <= pairs[i][0], pairs[i][1] < s.length
s
only contains lower case English letters.
Solution
smallest-string-with-swaps.py
class UnionFind:
def __init__(self):
self._parent = {}
self._size = {}
def union(self, a, b):
a, b = self.find(a), self.find(b)
if a == b:
return
if self._size[a] < self._size[b]:
a, b = b, a
self._parent[b] = a
self._size[a] += self._size[b]
def find(self, x):
if x not in self._parent:
self._parent[x] = x
self._size[x] = 1
while self._parent[x] != x:
self._parent[x] = self._parent[self._parent[x]]
x = self._parent[x]
return x
class Solution:
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
n = len(s)
uf = UnionFind()
res = [""] * n
for a, b in pairs:
uf.union(a, b)
groups = defaultdict(list)
indexes = defaultdict(list)
for i in range(n):
parent = uf.find(i)
groups[parent].append(s[i])
indexes[parent].append(i)
for g in groups.keys():
groups[g].sort()
indexes[g].sort()
for i, index in enumerate(indexes[g]):
res[index] = groups[g][i]
return "".join(res)