Swap-Locked Badge Normalizer
Preview mode. Log in to edit, run, submit, and save progress.
Swap-Locked Badge Normalizer
A badge printer stores a lowercase code string. The security team provides pairs of positions that may be swapped any number of times. If two positions are connected through a chain of allowed swaps, their characters can effectively be rearranged among all positions in that connected group. Characters cannot move between groups that are not connected by swap permissions. Input Format: You are given a string s and an array pairs, where each pair contains two swappable indices. Output Format: Return the lexicographically smallest badge code obtainable using any number of allowed swaps.
Examples
s = "dcab" pairs = [[0, 3], [1, 2]]
bacd
Explanation: indices [0, 3] use sorted letters ["b", "d"]; indices [1, 2] use sorted letters ["a", "c"]. This builds the smallest badge code "bacd", so the output is bacd.
Approach hint
Look for the state that actually matters after each operation.
Common mistake
Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.