Given a sentence text (A sentence is a string of space-separated words) in the following format:
text are separated by a single space.Your task is to rearrange the words in text such that all words are rearranged in an increasing order of their lengths. If two words have the same length, arrange them in their original order.
Return the new text following the format shown above.
Example 1:
Input: text = "Leetcode is cool" Output: "Is cool leetcode" Explanation: There are 3 words, "Leetcode" of length 8, "is" of length 2 and "cool" of length 4. Output is ordered by length and the new first word starts with capital letter.
Example 2:
Input: text = "Keep calm and code on" Output: "On and keep calm code" Explanation: Output is ordered as follows: "On" 2 letters. "and" 3 letters. "keep" 4 letters in case of tie order by position in original text. "calm" 4 letters. "code" 4 letters.
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^5This approach involves splitting the sentence into individual words and then using a stable sort method to arrange the words by their length. Languages typically have built-in sort functionalities which can use a custom key to sort based on the length of each word. The first word in the result then needs to be capitalized to maintain the sentence format.
The C solution involves splitting the string into words using strtok, storing them in an array, and sorting them using qsort. The custom comparator sorts by word length. After sorting, convert the first letter of the first word to uppercase and concatenate the words back into the sentence.
C++
Java
Python
C#
JavaScript
Time complexity: O(n log n) where n is the number of words due to sorting.
Space complexity: O(n) for storing the words.
This approach uses a custom method to first sort the words by length and then recreate the sentence. The main idea is to use a stable sorting algorithm that will retain the original order of words with the same length, and then to capitalize the first word using string manipulation methods available in each programming language.
Similar to the C solution in Approach 1, but clearly emphasizes separating the word capitalization step into a separate function. The words are extracted and stored in an array, sorted using qsort, and concatenated back with attention to capitalizing the first word.
C++
Java
Python
C#
JavaScript
Time complexity: O(n log n) for sorting.
Space complexity: O(n), mainly for the storage of words.
| Approach | Complexity |
|---|---|
| Sort Words by Length Using Built-in Sort | Time complexity: |
| Custom Sort by Length and Capitalize First | Time complexity: |
Sorting a Sentence (LeetCode 1859) | Solution with examples | Detail explanation | Study Algorithms • Nikhil Lohia • 9,216 views views
Watch 9 more video solutions →Practice Rearrange Words in a Sentence with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor