158. Smallest String With Swaps

Given a string s and pairs of indices pairs between which characters may be swapped. Return the lexicographically smallest string reachable by such swaps.
Example 1
Input: s = "dcab", pairs = [[0,3],[1,2]]
Output: "bacd"
Explanation: Swaps within connected components.
Example 2
Input: s = "cba", pairs = [[0,1],[1,2]]
Output: "abc"
Explanation: The entire string is in one component.
graph
JavaScript
Loading...
Line 1, Char 1