Smallest String With Swaps - Complete Solution Guide
Smallest String With Swaps is LeetCode problem 1202, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a string s , and an array of pairs of indices in the string pairs where pairs[i] = [a, b] indicates 2 indices(0-indexed) of the string. You can swap the characters at any pair of indices in the given pairs any number of times . Return the lexicographically smallest string that s can be changed to after using the swaps. Example 1: Input: s = "dcab", pairs = [[0,3],[1,2]] Output: "bacd" Explaination: Swap s[0] and s[3], s = "bcad" Swap s[1] and s[2], s = "bacd" Example 2: Input: s =
Detailed Explanation
The problem asks us to find the lexicographically smallest string that can be formed from a given string `s` by swapping characters at indices specified in the `pairs` array. We can swap characters at any pair of indices in `pairs` any number of times. The pairs represent connected components within the string. The goal is to rearrange characters within each connected component to produce the smallest possible string. For example, if we have 'dcab' and pairs [[0,3],[1,2]], then index 0 and 3 can swap, as well as 1 and 2. We can swap 'd' with 'b' to get 'bcad', and then 'c' with 'a' to get 'bacd'.
Solution Approach
The solution uses the Union-Find data structure to identify connected components within the string based on the given pairs of indices. For each connected component, it gathers the characters and their original indices. Then, it sorts both the characters and the indices. Finally, it reconstructs the string by placing the sorted characters at their corresponding sorted indices within the result string. This ensures that the smallest characters are placed at the smallest indices within each connected component.
Step-by-Step Algorithm
- Step 1: Initialize a Union-Find data structure (parent array and size array) to track connected components. Initially, each index is its own parent, representing separate components.
- Step 2: Iterate through the `pairs` array and perform the `union` operation on each pair of indices. The `union` operation merges the connected components of the two indices.
- Step 3: Create a dictionary (or HashMap in Java/C++) to store connected components. The key is the root (parent) of the component, and the value is a tuple (or pair) of two lists: a list of indices and a list of characters belonging to that component.
- Step 4: Iterate through the string `s` and, for each index, find the root of its connected component. Add the index and the corresponding character to the appropriate lists in the dictionary.
- Step 5: Iterate through the dictionary of connected components. For each component, sort the list of indices and the list of characters in ascending order.
- Step 6: Create a new string (or character array) to store the result. Iterate through the sorted lists of indices and characters for each component, and place each character at its corresponding index in the result string.
- Step 7: Convert the resulting character array (or string) and return it.
Key Insights
- Insight 1: The 'pairs' array defines connected components in the string. Indices within a component can be freely swapped.
- Insight 2: Union-Find (Disjoint Set) data structure is effective for identifying connected components.
- Insight 3: Sorting the indices and characters within each component allows creating the lexicographically smallest string.
Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, String, Depth-First Search, Breadth-First Search, Union Find, Sorting.
Companies
Asked at: PhonePe.