Practice Rearrange Words in a Sentence
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Reorder the words in text by increasing length, retaining their original relative order when lengths tie. The input is a sentence with one space between words and a capitalized first letter. Return the reordered sentence in the same format: only its first letter is uppercase, and words have single spaces between them.
Source: Rearrange Words in a Sentence on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
Input
text = "Leetcode is cool"
Output
"Is cool leetcode"
# Example 2
Input
text = "Keep calm and code on"
Output
"On and keep calm code"
# Example 3
Input
text = "To be or not to be"
Output
"To be or to be not"
Constraints
text begins with a capital letter and then contains lowercase letters and single space between words.
1 <= text.length <= 10^5
Related LeetCode Problems
Practice more problems similar to Rearrange Words in a Sentence.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.