Lexicographically Smallest String After Reverse II Problem
Description
LeetCode Problem 3735.
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:
- reverse the first
kcharacters ofs, or - reverse the last
kcharacters ofs.
Return the lexicographically smallest string that can be obtained after exactly one such operation.
Example 1:
Input: s = "dcab"
Output: "acdb"
Explanation:
- Choose k = 3, reverse the first 3 characters.
- Reverse "dca" to "acd", resulting string s = "acdb", which is the lexicographically smallest string achievable.
Example 2:
Input: s = "abba"
Output: "aabb"
Explanation:
- Choose k = 3, reverse the last 3 characters.
- Reverse "bba" to "abb", so the resulting string is "aabb", which is the lexicographically smallest string achievable.
Example 3:
Input: s = "zxy"
Output: "xzy"
Explanation:
- Choose k = 2, reverse the first 2 characters.
- Reverse "zx" to "xz", so the resulting string is "xzy", which is the lexicographically smallest string achievable.
Constraints:
1 <= n == s.length <= 10^5sconsists of lowercase English letters.
Solution
Approach: Hashing + Binary Search to Compare Candidates
Let t be s reversed. Reversing the first k characters gives t[n-k:] + s[k:], and reversing the last k gives s[:n-k] + t[:k]. So all 2n candidates are a piece of s or t followed by a piece of s or t. Trying all of them and comparing them character by character would take O(n^2), which is too slow for n = 10^5.
Instead compare two candidates with polynomial hashing. Precompute prefix hashes of s and t; then the hash of any prefix of a candidate (at most two pieces) can be computed in O(1). Binary search for the longest common prefix of the two candidates, then compare the next characters. Keep the smallest candidate seen (starting from s itself, which is the k = 1 result). A cheap check of the first character skips most candidates early.
Complexity: time O(n log n), space O(n).
class Solution:
def lexSmallest(self, s: str) -> str:
n = len(s)
t = s[::-1]
M, B = (1 << 61) - 1, 911382323
words = [s, t]
pw = [1] * (n + 1)
for i in range(n):
pw[i + 1] = pw[i] * B % M
hs = []
for w in words:
h = [0] * (n + 1)
for i, ch in enumerate(w):
h[i + 1] = (h[i] * B + ord(ch)) % M
hs.append(h)
# A candidate is words[a][xo:xo+L] + words[b][yo:yo+n-L].
def prefix_hash(c, m):
a, xo, L, b, yo = c
if m <= L:
h = hs[a]
return (h[xo + m] - h[xo] * pw[m]) % M
h1, h2 = hs[a], hs[b]
x = (h1[xo + L] - h1[xo] * pw[L]) % M
y = (h2[yo + m - L] - h2[yo] * pw[m - L]) % M
return (x * pw[m - L] + y) % M
def char_at(c, i):
a, xo, L, b, yo = c
return words[a][xo + i] if i < L else words[b][yo + i - L]
def less(c, d):
lo, hi = 0, n # longest common prefix length
while lo < hi:
mid = (lo + hi + 1) >> 1
if prefix_hash(c, mid) == prefix_hash(d, mid):
lo = mid
else:
hi = mid - 1
return lo < n and char_at(c, lo) < char_at(d, lo)
best = (1, n - 1, 1, 0, 1) # k = 1: s itself
for k in range(1, n + 1):
for c in ((1, n - k, k, 0, k), (0, 0, n - k, 1, 0)):
if char_at(c, 0) <= char_at(best, 0) and less(c, best):
best = c
a, xo, L, b, yo = best
return words[a][xo:xo + L] + words[b][yo:yo + n - L]