↓Skip to main content
  1. LeetCode/

LeetCode 1202: Smallest String With Swaps

·1 min· ·
LeetCode Medium Union-Find
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-05-06

  • Total time: 10:00.00

Intuition
#

The problem requires returning the lexicographically smallest string after a series of allowed swaps.
The swap condition is that two characters can be swapped only if their indices are in the same pair.
This implies that we can use Union Find to group indices into connected components.
Then, for each group, we sort the characters and fill the result from smallest to largest.

Approach
#

class Solution:
    def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
        uf = UF(s)

        for x, y in pairs:
            uf.union(x, y)

        group_map = defaultdict(list)
        for idx in range(len(s)):
            group_map[uf.find(idx)].append(s[idx])
        
        for i in group_map:
            group_map[i] = sorted(group_map[i], reverse=True)

        s = []
        for parent in uf.parent:
            group = uf.find(parent)
            s.append(group_map[group].pop())

        return "".join(s)


class UF:
    def __init__(self, s):
        n = len(s)
        self.parent = [i for i in range(n)]
        self.ranking = [1] * n

    def find(self, num):

        if self.parent[num] != num:
            self.parent[num] = self.find(self.parent[num])

        return self.parent[num]

    def union(self, x, y):
        root_x = self.find(x)
        root_y = self.find(y)

        if root_x == root_y:
            return

        if self.ranking[root_x] > self.ranking[root_y]:
            self.parent[root_y] = self.parent[root_x]
            self.ranking[root_x] += self.ranking[root_y]

        else:
            self.parent[root_x] = self.parent[root_y]
            self.ranking[root_y] += self.ranking[root_x]

Findings
#

Encountered Problems
#

Related

LeetCode 1319: Number of Operations to Make Network Connected
·2 mins
LeetCode Medium Union-Find
LeetCode Problem Solving
LeetCode 547: Number of Provinces
·3 mins
LeetCode Medium Union-Find Dfs
LeetCode Problem Solving
LeetCode 684: Redundant Connection
·1 min
LeetCode Daily Medium Union-Find
LeetCode Problem Solving
LeetCode 3493: Properties Graph
·2 mins
LeetCode Medium Graph Dfs Union-Find Hash-Map
LeetCode 3493 note: convert each row to a set, compare intersection sizes pairwise to add edges, then count components
LeetCode 695: Max Area of Island
·2 mins
LeetCode Medium Array Depth-First-Search Breadth-First-Search Union-Find
LeetCode 695 note: DFS each unvisited 1 to measure its island and keep the maximum; cells are the integer 1, not a string
LeetCode 128: Longest Consecutive Sequence
·2 mins
LeetCode Medium Array Hash-Table Union-Find
LeetCode 128 note: put everything in a set and count upward only from values whose x - 1 is absent; each value is seen at most twice, O(n)