To compare two words syllabically, first decompose them into sequences of syllables. For example, the words "zcdbadaerfe" and "foubsyudba" decompose as follows:
Then, sort each sequence of syllables alphabetically. In the above example, the sequences become:
Then, compare these sorted sequences lexicographically. A sequence S1 comes before a sequence S2 lexicographically if S1 has an alphabetically earlier element at the first index at which they differ. In the above example, the second sequence comes earlier lexicographically because "bsyu" comes before "dae" alphabetically.
If two sorted sequences are equal, then compare their corresponding unsorted sequences instead. For example, the words "daba" and "bada" decompose into the same sorted sequence {"ba", "da"}. Compare the unsorted sequences {"da", "ba"} and {"ba", "da"} to determine that "bada" comes before "daba".
You are given a String[] words. Sort the words using the
method above and return the resulting String[].
words will contain between 1 and 50 elements, inclusive.
words will contain between 2 and 50 characters,
inclusive.
words will contain only lowercase letters
('a'-'z').
words will be a consonant.
words will be a vowel.
{"xiaoxiao", "yamagawa", "gawayama"}
Returns: {"gawayama", "yamagawa", "xiaoxiao" }
After decomposing the words into sequences of syllables, we get the
following unsorted and sorted sequences of syllables:
WORD | UNSORTED SEQUENCES | SORTED SEQUENCES
-----------+--------------------------+--------------------------
"xiaoxiao" | {"xiao", "xiao"} | {"xiao", "xiao"}
"yamagawa" | {"ya", "ma", "ga", "wa"} | {"ga", "ma", "wa", "ya"}
"gawayama" | {"ga", "wa", "ya", "ma"} | {"ga", "ma", "wa", "ya"}
To compare "xiaoxiao" with the other two words, we use the sorted
sequences of syllables. However, to compare "yamagawa" with
"gawayama" we have to use the unsorted sequences because the sorted
ones are equal.
{"bcedba", "dbabce", "zyuxxo"}
Returns: {"bcedba", "dbabce", "zyuxxo" }
{"hgnibqqaxeiuteuuvksi", "jxbuzui", "zrotyqeruiydozui",
"ywuuzkto", "lmopbookoagyco", "vredfvavvexliu"}
Returns:
{"hgnibqqaxeiuteuuvksi",
"vredfvavvexliu",
"lmopbookoagyco",
"jxbuzui",
"zrotyqeruiydozui",
"ywuuzkto" }
{"crazgo", "cwsoygiokiuo", "yueoseeu", "tuadiojvugeoe",
"naumxditui", "sgukkelyoi", "nrohjuasoia", "mgabmo"}
Returns:
{"mgabmo",
"crazgo",
"cwsoygiokiuo",
"tuadiojvugeoe",
"nrohjuasoia",
"sgukkelyoi",
"naumxditui",
"yueoseeu" }
{"wheewjuguoi", "coutcu", "hqivaa", "sgiibgwi", "ypaqpki",
"bgyikouapae", "wqakeu", "skolfo", "pzesaa", "ypivhi"}
Returns:
{"sgiibgwi",
"bgyikouapae",
"coutcu",
"wheewjuguoi",
"hqivaa",
"wqakeu",
"skolfo",
"pzesaa",
"ypaqpki",
"ypivhi" }