You are given a string s of length n consisting of lowercase English letters.
You must perform exactly one operation by choosing any integer k such that 1 <= k <= n and either:
k characters of s, ork characters of s.Return the lexicographically smallest string that can be obtained after exactly one such operation.
Example 1:
Input: s = "dcab"
Output: "acdb"
Explanation:
k = 3, reverse the first 3 characters."dca" to "acd", resulting string s = "acdb", which is the lexicographically smallest string achievable.Example 2:
Input: s = "abba"
Output: "aabb"
Explanation:
k = 3, reverse the last 3 characters."bba" to "abb", so the resulting string is "aabb", which is the lexicographically smallest string achievable.Example 3:
Input: s = "zxy"
Output: "xzy"
Explanation:
k = 2, reverse the first 2 characters."zx" to "xz", so the resulting string is "xzy", which is the lexicographically smallest string achievable.
Constraints:
1 <= n == s.length <= 105s consists of lowercase English letters.Problem Overview: You are given a string and can reverse exactly one substring. The goal is to produce the lexicographically smallest possible string after the operation. The challenge is efficiently comparing many candidate strings without rebuilding and comparing full strings each time.
Approach 1: Brute Force Enumeration (O(n^3) time, O(n) space)
Try every possible substring [i, j], reverse it, and compare the resulting string with the current best. There are O(n^2) substrings and each comparison can take O(n). This leads to O(n^3) total time. The approach is straightforward and useful for reasoning about the problem, but it quickly becomes impractical for large inputs.
Approach 2: Rolling Hash + Binary Search Comparison (O(n log n) time, O(n) space)
The key observation is that most candidate strings share long prefixes. Instead of building full strings, compare them using prefix hashes. Precompute polynomial hashes for the original string and its reversed version using a hash function. When evaluating a candidate reversal, use binary search to find the longest common prefix between the current best string and the candidate. Rolling hash lets you compare substrings in O(1), so each comparison costs O(log n). Iterating over possible reversal boundaries while performing hashed comparisons reduces the total complexity dramatically.
Approach 3: Suffix Array Based Comparison (O(n log n) preprocessing, O(n log n) total)
Another method builds a suffix array and LCP structure for fast lexicographic comparisons. Each candidate produced by reversing a segment can be compared using suffix ranks and LCP queries rather than character-by-character checks. The preprocessing cost is higher, but once built, comparisons become efficient. This approach is more common in competitive programming environments where multiple string comparisons are required.
Recommended for interviews: The rolling hash + binary search approach usually strikes the best balance. Brute force demonstrates understanding of the transformation, but it fails for large constraints. Using hashed substring comparison shows you understand how to optimize lexicographic comparisons in String problems and handle many candidate transformations efficiently.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Substring Reversal | O(n^3) | O(n) | Small inputs or verifying correctness during prototyping |
| Rolling Hash + Binary Search Comparison | O(n log n) | O(n) | General case with large strings and many candidate comparisons |
| Suffix Array + LCP Queries | O(n log n) | O(n) | When suffix array infrastructure already exists or multiple lexicographic comparisons are needed |
Practice Lexicographically Smallest String After Reverse II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor