Graph Theory37 sections · 1633 units
Open in Course

LeetCode 1202 Smallest String With Swaps - Problem Statement

LeetCode 1202

Problem: Smallest String With Swaps Task: Given string ss and swap pairs, find the lexicographically smallest string achievable. Within each connected component of indices, you can arrange characters in any order.

Group indices by DSU, sort characters within each group, and place them back in sorted order. The insight: if you can swap index ii with jj, and jj with kk, then transitivity lets you swap ii with kk too. Connected components capture this transitive closure.