#K37917. Word Sorting Challenge
Word Sorting Challenge
Word Sorting Challenge
Given a string consisting of words separated by spaces, your task is to sort the words based on their lengths in ascending order. When two words have the same length, sort them in lexicographical order. The output should be the rearranged words separated by a single space.
More formally, let \( S = \{w_1, w_2, \dots, w_n\} \) be the set of words from the input string. You need to produce a sequence \( S' \) such that for any two words \( a \) and \( b \) in \( S' \):
\( |a| < |b| \) or \( (|a| = |b| \text{ and } a < b) \).
If the input string is empty, output an empty string.
inputFormat
The input consists of a single line containing a sequence of words separated by spaces. The line can be empty.
Input Format:
A single line read from standard input.
outputFormat
Output the sorted sequence of words as a single line to standard output. Words must be separated by a single space. If the input is empty, output an empty line.
## samplepear apple banana grape orange kiwi
kiwi pear apple grape banana orange