Sorting the Sentence - Problem
A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters.
A sentence can be shuffled by appending the 1-indexed word position to each word then rearranging the words in the sentence.
For example, the sentence "This is a sentence" can be shuffled as "sentence4 a3 is2 This1" or "is2 sentence4 This1 a3".
Given a shuffled sentence s containing no more than 9 words, reconstruct and return the original sentence.
Input & Output
Example 1 — Basic Case
$
Input:
s = "is2 sentence4 This1 Test5 est6"
›
Output:
"This is sentence sentence Test est"
💡 Note:
Sort words by their position digits: This1→position 1, is2→position 2, sentence4→position 4, Test5→position 5, est6→position 6.
Example 2 — Different Order
$
Input:
s = "Myself2 Me1 I4 and3"
›
Output:
"Me Myself and I"
💡 Note:
Rearrange by positions: Me1, Myself2, and3, I4 becomes "Me Myself and I"
Example 3 — Single Word
$
Input:
s = "T4est1"
›
Output:
"T4est"
💡 Note:
Single word with position 1, just remove the digit to get "T4est"
Constraints
-
scontains only lowercase and uppercase English letters, spaces and digits from 1 to 9 -
All the words in
sare separated by a single space -
scontains between 1 and 9 words, inclusive -
Each word in
sconsists of lowercase and uppercase English letters - All the words have a unique position digit
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code