#1859

Sorting the Sentence

newbie · 130 · lc easy +13 · verified · 84.1% accepted · 2,389 likes · top 95%

Description

A sentence is a list of words separated by single spaces with no leading or trailing spaces. A shuffled sentence is formed by appending each word's 1-based position to the word and reordering the words arbitrarily.

Given a shuffled sentence s (at most 9 words), reconstruct and return the original sentence.

Example 1:

Input: s = "is2 sentence4 This1 a3"
Output: "This is a sentence"
Explanation: Sort the words in s to their original positions "This1 is2 a3 sentence4", then remove the numbers.

Example 2:

Input: s = "Myself2 Me1 I4 and3"
Output: "Me Myself and I"
Explanation: Sort the words in s to their original positions "Me1 Myself2 and3 I4", then remove the numbers.

Code

1
2
3