- Identify the ordered answer range or sorted search domain.
- Write a predicate whose truth changes only once.
- Move the appropriate boundary after each midpoint check and return the final feasible position.
Code notes
- 130 lines of C++ from the credited upstream file lexicographically-smallest-string-after-reverse.cpp.
- The implementation visibly relies on sequence storage.
- 8 loop blocks detected.
Complexity
Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123 45class Solution {6public:7 string lexSmallest(string s) {8 static const int64_t MOD = 1e9 + 7;9 static const int64_t B = 29;10 const int n = size(s);11 12 const auto& binary_search = [](auto left, auto right, const auto& check) {13 while (left <= right) {14 const auto mid = left + (right - left) / 2;15 if (check(mid)) {16 right = mid - 1;17 } else {18 left = mid + 1;19 }20 }21 return left;22 };23 24 vector<int64_t> prefix(n + 1);25 for (int i = 0; i + 1 < size(prefix); ++i) {26 prefix[i + 1] = (prefix[i] * B + s[i]) % MOD;27 }28 vector<int64_t> suffix(n + 1);29 for (int i = size(suffix) - 2; i >= 0; --i) {30 suffix[i] = (suffix[i + 1] * B + s[i]) % MOD;31 }32 vector<int64_t> base(n + 1, 1);33 for (int i = 0; i + 1 < size(base); ++i) {34 base[i + 1] = (base[i] * B) % MOD;35 }36 37 const auto& get_prefix_hash = [&](int l, int r) {38 if (l > r) {39 return static_cast<int64_t>(0);40 }41 return (prefix[r + 1] - prefix[l] * base[r - l + 1] % MOD + MOD) % MOD;42 };43 44 const auto& get_suffix_hash = [&](int l, int r) {45 if (l > r) {46 return static_cast<int64_t>(0);47 }48 return (suffix[l] - suffix[r + 1] * base[r - l + 1] % MOD + MOD) % MOD;49 };50 51 const auto& get_total_hash = [&](int k, int t, int l) {52 if (!t) {53 return l <= k54 ? get_suffix_hash(k - l, k - 1)55 : ((get_suffix_hash(0, k - 1) * base[l - k]) % MOD + get_prefix_hash(k, l - 1)) % MOD;56 }57 const auto& nk = n - k;58 return l <= nk59 ? get_prefix_hash(0, l - 1)60 : ((get_prefix_hash(0, nk - 1) * base[l - nk]) % MOD + get_suffix_hash(n - (l - nk), n - 1)) % MOD;61 };62 63 const auto& get_char = [&](int k, int t, int idx) {64 if (!t) {65 return idx < k66 ? s[(k - 1) - idx]67 : s[idx];68 }69 return idx < n - k70 ? s[idx]71 : s[(n - 1) - (idx - (n - k))];72 };73 74 int best_k = 1, best_i = 0;75 auto is_less = [&](int k, int i) {76 const auto& idx = binary_search(0, n - 1, [&](int x) {77 return get_total_hash(k, i, x + 1) != get_total_hash(best_k, best_i, x + 1);78 });79 return idx != n && get_char(k, i, idx) < get_char(best_k, best_i, idx);80 };81 const auto& mn = ranges::min(s);82 for (int k = 1; k <= n; ++k) {83 if (s[k - 1] != mn) {84 continue;85 }86 if (is_less(k, 0)) {87 best_k = k;88 best_i = 0;89 }90 }91 for (int k = 1; k <= n; ++k) {92 if (!(s[size(s) - k] >= s.back())) {93 continue;94 }95 if (is_less(k, 1)) {96 best_k = k;97 best_i = 1;98 }99 }100 string result(s);101 if (!best_i) {102 reverse(begin(result), begin(result) + best_k);103 } else {104 reverse(begin(result) + (size(result) - best_k), end(result));105 }106 return result;107 }108};109 110111112113class Solution2 {114public:115 string lexSmallest(string s) {116 string result = s;117 for (int k = 2; k <= size(s); ++k) {118 string t = s;119 reverse(begin(t), begin(t) + k);120 result = min(result, t);121 }122 for (int k = 2; k <= size(s); ++k) {123 string t = s;124 reverse(begin(t) + (size(t) - k), end(t));125 result = min(result, t);126 }127 return result;128 }129};130