Skip to content

Practice Sorting the Sentence

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#1859
LeetCode Problem

Sorting the Sentence

Problem Statement

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. Given a shuffled sentence s containing no more than 9 words, reconstruct and return the original sentence.

Source: Sorting the Sentence on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "is2 sentence4 This1 a3"
Output
"This is a sentence"

# Example 2

Input
s = "Myself2 Me1 I4 and3"
Output
"Me Myself and I"

Constraints

2 <= s.length <= 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.

Related LeetCode Problems

Practice more problems similar to Sorting the Sentence.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.