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.
"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.
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.
Constraints:
2 <= s.length <= 200s consists of lowercase and uppercase English letters, spaces, and digits from 1 to 9.s is between 1 and 9.s are separated by a single space.s contains no leading or trailing spaces.Problem Overview: You receive a shuffled sentence where each word ends with a digit representing its correct position in the sentence. Your task is to reorder the words based on these indices and return the original sentence without the digits.
The input looks like "is2 sentence4 This1 a3". Each word contains its position at the end. The goal is to place every word in its correct position and remove the trailing number.
Approach 1: Sorting + String Manipulation (O(n log n) time, O(n) space)
Split the sentence into words using whitespace. Each word contains its index as the last character, so you can extract that digit and use it as the sorting key. After sorting the array of words by their index, remove the trailing digit from each word and join them back into a sentence. This approach relies on built‑in sorting from the sorting category and basic string operations. The implementation is straightforward and readable, but the sort operation introduces O(n log n) time complexity.
Approach 2: Array Indexing (O(n) time, O(n) space)
The index digit at the end of each word already tells you the exact final position. Instead of sorting, create an array of size n where n is the number of words. Iterate through each word, extract the index, remove the digit, and place the word directly at result[index - 1]. After processing all words, join the array into the final sentence. This method avoids sorting completely and processes each word exactly once, achieving O(n) time. The approach uses simple array placement and string slicing.
The key insight is that the index digit encodes the final position explicitly. Sorting works because the digit defines order, but direct indexing is faster because it skips the comparison step entirely.
Recommended for interviews: Interviewers typically expect the linear-time array indexing solution. It demonstrates that you noticed the positional information embedded in each word and used it to avoid sorting. Showing the sorting approach first proves you understand the problem quickly, but implementing the O(n) direct placement solution shows stronger algorithmic thinking.
This approach involves splitting the input sentence into words, then individually processing each word to extract the index number, and finally sorting them based on these indices.
Once sorted, the index numbers can be removed, and the words rejoined to form the original sentence.
This Python solution first splits the input sentence into a list of words. It then sorts these words using a custom key function that extracts the last character (the positional index) from each word. Finally, it constructs the original sentence by removing these index numbers and joining the words.
Python
JavaScript
Java
Time Complexity: O(n log n), where n is the number of words, due to the sort operation.
Space Complexity: O(n), for storing the split words.
This method uses an array to directly place each word into its correct position using the appended index number. Once positioned correctly, the numbers are removed to form the original sentence.
The C++ solution processes each word to determine its position directly using the appended index. It places them into a predefined vector and constructs the output sentence by ignoring empty slots and joining the words.
Time Complexity: O(n), where n is the number of characters in the sentence.
Space Complexity: O(1), apart from the output string which is fixed sized due to constraint.
First, we split the string s by spaces to get the array of strings ws. Then, we iterate through the array ws, subtracting the character '1' from the last character of each word to get the result as the index of the word. We take the prefix of the word as the content of the word. Finally, we concatenate the words in index order.
The time complexity is O(n), and the space complexity is O(n), where n is the length of the string s.
Python
Java
C++
Go
TypeScript
JavaScript
| Approach | Complexity |
|---|---|
| Using Sorting and String Manipulation | Time Complexity: O(n log n), where n is the number of words, due to the sort operation. |
| Using Array Indexing | Time Complexity: O(n), where n is the number of characters in the sentence. |
| String Splitting | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sorting + String Manipulation | O(n log n) | O(n) | Quick and readable solution using built-in sorting |
| Array Indexing (Direct Placement) | O(n) | O(n) | Optimal approach when positions are encoded in the data |
Leetcode 1859. Sorting the Sentence || Biweekly Contest 52 problem 1 || Code + Explanation + Example • Code with Alisha • 14,045 views views
Watch 9 more video solutions →Practice Sorting the Sentence with our built-in code editor and test cases.
Practice on FleetCode