#C14987. Smallest Lexicographical String After Reversals
Smallest Lexicographical String After Reversals
Smallest Lexicographical String After Reversals
Given a string ( s ) consisting of lowercase English letters, you are allowed to perform any number of substring reversals. In a reversal, you can choose any contiguous substring of ( s ) and reverse its characters. Your task is to find and output the lexicographically smallest string that can be obtained by applying any sequence of such operations.
A key observation is that no matter how you reverse substrings, you can always rearrange the string into its sorted order. Thus, the lexicographically smallest string is simply the sorted version of ( s ).
For example:
- Input:
dcba
→ Output:abcd
- Input:
bca
→ Output:abc
- Input:
zxy
→ Output:xyz
Your program should read a string from standard input and output the lexicographically smallest string to standard output.
inputFormat
The input consists of a single line containing the string ( s ) (only lowercase English letters).
outputFormat
Output the lexicographically smallest string possible by performing any number of substring reversals on the input string.## sample
dcba
abcd