#3722

Lexicographically Smallest String After Reverse

medium · 54.7% accepted · 36 likes · top 47%

two pointers · binary search · enumeration

Description

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 k characters of s, or

- reverse the last k characters of s.

Return the lexicographically smallest string that can be obtained after exactly one such operation.

Solution