Sorting the Sentence

A sentence is a list of words separated by single spaces, with no leading or trailing spaces. Each word is made of lowercase and uppercase English letters. A sentence can be **shuffled** by appending the 1-indexed position of each word to it and then rearranging the words. For example, `"This is a sentence"` can become `"sentence4 a3 is2 This1"`. Given a shuffled sentence `s` with no more than 9 words, rebuild and return the original sentence.

30:00

Sorting the Sentence
easy
Free written solution
Topics
Companies

A sentence is a list of words separated by single spaces, with no leading or trailing spaces. Each word is made of lowercase and uppercase English letters.

A sentence can be shuffled by appending the 1-indexed position of each word to it and then rearranging the words. For example, "This is a sentence" can become "sentence4 a3 is2 This1".

Given a shuffled sentence s with no more than 9 words, rebuild and return the original sentence.

Example 1:
Input: {"s":"is2 sentence4 This1 a3"}
Output: This is a sentence
Constraints:
  • 2≤s.length≤2002 \leq s.\text{length} \leq 200

  • s consists of lowercase and uppercase English letters, spaces, and digits from 1 to 9.

  • The number of words in s is between 1 and 9.

  • The words in s are separated by a single space.

  • s contains no leading or trailing spaces.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"is2 sentence4 This1 a3"}

Split the sentence on spaces: 4 shuffled words, each carrying its position as a last digit.

shuffled words (last character = position)

is

2

sentence

4

This

1

a

3

slots by position

not created yet

sentence

___ ___ ___ ___

Variables
VariableValue
words4
DepthFunction Call
Stack empty
0/10