 All Problems
Smallest String With Swaps
medium
union-find
hash table
string
amazon
google

You are given a string s and a list of pairs of indices pairs where you can swap characters at those indices any number of times.

Return the lexicographically smallest string that s can be changed to.

Example 1:

Input: s = "dcab", pairs = [[0,3],[1,2]]
Output: "bacd"

Example 2:

Input: s = "dcab", pairs = [[0,3],[1,2],[0,2]]
Output: "abcd"

Constraints:

  • 1 <= s.length <= 10^5
  • 0 <= pairs.length <= 10^5
Run to check your code against the sample cases, or submit to run every case