![]() |
VOOZH | about |
Given a string consisting of lowercase letters, arrange all its letters in ascending order.
Examples:
Input: s = "edcab"
Output: "abcde"
Explanation: characters are in ascending order in "abcde".
Input: s = "xzy"
Output: "xyz"
Explanation: characters are in ascending order in "xyz".
Table of Content
The idea is to sort all characters of the string directly using built in sorting method. Since sorting arranges characters in ascending order, the resulting string becomes the required answer.
xyz
Time Complexity: O(n log n)
Auxiliary Space: O(1)
The idea is to count how many times each lowercase letter appears in the string. Since there are only 26 possible lowercase characters, we can store their frequencies and then rebuild the string from
'a'to'z'according to those frequencies.
Let us understand with example:
Input: s = "xzy"
xyz
Time Complexity: O(n)
Auxiliary Space: O(1)