#C14535. Alphabetically Rearranging Words
Alphabetically Rearranging Words
Alphabetically Rearranging Words
You are given a string s consisting of multiple words separated by single spaces. Your task is to rearrange the words in the string in alphabetical order while ignoring case sensitivity.
The rearrangement should be stable: if two words are equal when compared in a case-insensitive manner, their original order should be preserved in the output.
For example, if the input is "Banana apple Orange
", the output should be "apple Banana Orange
" because when sorting without case consideration, "apple" comes before "Banana" and "Orange".
Note: The input will be provided via stdin
and the output should be written to stdout
. The input string will be a single line.
In mathematical terms, if you denote the set of words as \(\{w_1, w_2, \dots, w_n\}\) and define the case-insensitive function \(f(w)=\text{lowercase}(w)\), then you are to output a permutation \(\{w_{p(1)},w_{p(2)},\dots,w_{p(n)}\}\) such that \[ f(w_{p(1)}) \le f(w_{p(2)}) \le \cdots \le f(w_{p(n)}), \] with stability preserved when \(f(w_{p(i)}) = f(w_{p(i+1)})\).
inputFormat
The input consists of a single line containing the string s (possibly empty), where the words are separated by a single space.
outputFormat
Output a single line containing the words rearranged in alphabetical order based on a case-insensitive comparison. Words that are equal ignoring case should retain their original order.
## samplebanana apple orange
apple banana orange