3722. Lexicographically Smallest String After Reverse Medium

@problem@discussion
#Two Pointers#Binary Search#Enumeration



1/**
2 * [3722] Lexicographically Smallest String After Reverse
3 *
4 * You are given a string s of length n consisting of lowercase English letters.
5 * You must perform exactly one operation by choosing any integer k such that 1 <= k <= n and either:
6 * 
7 * 	reverse the first k characters of s, or
8 * 	reverse the last k characters of s.
9 * 
10 * Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> string that can be obtained after exactly one such operation.
11 *  
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">s = "dcab"</span>
15 * Output: <span class="example-io">"acdb"</span>
16 * Explanation:
17 * 
18 * 	Choose k = 3, reverse the first 3 characters.
19 * 	Reverse "dca" to "acd", resulting string s = "acdb", which is the lexicographically smallest string achievable.
20 * </div>
21 * <strong class="example">Example 2:
22 * <div class="example-block">
23 * Input: <span class="example-io">s = "abba"</span>
24 * Output: <span class="example-io">"aabb"</span>
25 * Explanation:
26 * 
27 * 	Choose k = 3, reverse the last 3 characters.
28 * 	Reverse "bba" to "abb", so the resulting string is "aabb", which is the lexicographically smallest string achievable.
29 * </div>
30 * <strong class="example">Example 3:
31 * <div class="example-block">
32 * Input: <span class="example-io">s = "zxy"</span>
33 * Output: <span class="example-io">"xzy"</span>
34 * Explanation:
35 * 
36 * 	Choose k = 2, reverse the first 2 characters.
37 * 	Reverse "zx" to "xz", so the resulting string is "xzy", which is the lexicographically smallest string achievable.
38 * </div>
39 *  
40 * Constraints:
41 * 
42 * 	1 <= n == s.length <= 1000
43 * 	s consists of lowercase English letters.
44 * 
45 */
46pub struct Solution {}
47
48// problem: https://leetcode.com/problems/lexicographically-smallest-string-after-reverse/
49// discuss: https://leetcode.com/problems/lexicographically-smallest-string-after-reverse/discuss/?currentPage=1&orderBy=most_votes&query=
50
51// submission codes start here
52
53impl Solution {
54    pub fn lex_smallest(s: String) -> String {
55        String::new()
56    }
57}
58
59// submission codes end
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_3722() {
67    }
68}
69

Back
© 2026 bowen.ge All Rights Reserved.