#K7651. Lexicographically Smallest String
Lexicographically Smallest String
Lexicographically Smallest String
You are given a string s
of length n
consisting of lowercase English letters. You are allowed to perform any number of adjacent swaps on the string. Your task is to output the lexicographically smallest string that can be obtained by these swaps. Essentially, this amounts to sorting the string in non-decreasing order.
Formally, given a string \( S = s_1s_2\ldots s_n \), you need to find the string \( T \) such that \( T = \text{sorted}(S) \) where the sorting is based on the lexicographical order. The expected time complexity is \( O(n \log n) \), which is efficient for the given constraints.
inputFormat
The input is given via standard input and consists of two lines:
- The first line contains an integer
n
indicating the length of the string. - The second line contains a string
s
of lengthn
consisting only of lowercase English letters.
outputFormat
Output via standard output the lexicographically smallest string obtainable by performing any number of adjacent swaps on s
. In other words, output the sorted version of the input string.
5
dcba
abcd
</p>