Lexicographically Smallest String After a Swap - Complete Solution Guide
Lexicographically Smallest String After a Swap is LeetCode problem 3216, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a string s containing only digits, return the lexicographically smallest string that can be obtained after swapping adjacent digits in s with the same parity at most once . Digits have the same parity if both are odd or both are even. For example, 5 and 9, as well as 2 and 4, have the same parity, while 6 and 9 do not. Example 1: Input: s = "45320" Output: "43520" Explanation: s[1] == '5' and s[2] == '3' both have the same parity, and swapping them results in the lexicographically smallest
Detailed Explanation
The problem asks you to find the lexicographically smallest string that can be created by swapping adjacent digits in the input string `s` that have the same parity (both even or both odd). The catch is that you can only perform at most one swap. Lexicographically smallest means the string that would come earliest in a dictionary ordering. For example, "123" is lexicographically smaller than "213". The input string `s` consists only of digits.
Solution Approach
The provided solutions all use a brute-force approach with nested loops. The outer loop iterates through each digit of the string. The inner loop searches for the smallest digit with the same parity to the right of the current digit. If a smaller digit is found, the two digits are swapped. This process guarantees that after the loops, the string is the lexicographically smallest possible string achievable through at most one swap of adjacent digits with the same parity.
Step-by-Step Algorithm
- Convert the input string `s` into a list of characters (or use `toCharArray()` in Java).
- Iterate through each character of the list using a nested loop. The outer loop index `i` represents the current digit.
- Inner loop iterates from `i + 1` to the end of the string, comparing the parity (even or odd) of the current digit `s[i]` with each subsequent digit `s[j]`.
- If a digit `s[j]` with the same parity and smaller value than the current smallest found digit is found, update `best_idx` (or keep track of the smallest character).
- After the inner loop, if a smaller digit with the same parity was found (`best_idx != i`), swap the digits at indices `i` and `best_idx`.
- Finally, convert the modified character list back into a string and return it.
Key Insights
- The problem involves finding the lexicographically smallest string, which implies a greedy approach: we should prioritize swapping smaller digits into earlier positions.
- We need to iterate through the string and, for each digit, find the smallest digit with the same parity that can be swapped with it.
- The constraint of swapping only adjacent digits simplifies the search space and makes a nested loop approach feasible.
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n)
Topics
This problem involves: String, Greedy.
Companies
Asked at: J.P. Morgan.