Class Solution
-
- All Implemented Interfaces:
public final class Solution3722 - Lexicographically Smallest String After Reverse.
Medium
You are given a string
sof lengthnconsisting of lowercase English letters.You must perform exactly one operation by choosing any integer
ksuch that1 <= k <= nand either:reverse the first
kcharacters ofs, orreverse the last
kcharacters ofs.
Return the lexicographically smallest string that can be obtained after exactly one such operation.
A string
ais lexicographically smaller than a stringbif, at the first position where they differ,ahas a letter that appears earlier in the alphabet than the corresponding letter inb. If the firstmin(a.length, b.length)characters are the same, then the shorter string is considered lexicographically smaller.Example 1:
Input: s = "dcab"
Output: "acdb"
Explanation:
Choose
k = 3, reverse the first 3 characters.Reverse
"dca"to"acd", resulting strings = "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 <= 1000sconsists of lowercase English letters.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final StringlexSmallest(String s)-
-
Method Detail
-
lexSmallest
final String lexSmallest(String s)
-
-
-
-