Leetcode Problem 1451. Rearrange Words in a Sentence

1451. Rearrange Words in a Sentence

Leetcode Solutions

Sorting Words by Length While Maintaining Original Order

  1. Convert the first character of the input text to lowercase.
  2. Split the text into words.
  3. Store each word along with its original index in a list of tuples.
  4. Sort the list of tuples primarily by the length of the words and secondarily by their original indices.
  5. Join the sorted words into a new sentence.
  6. Capitalize the first character of the new sentence.
  7. Return the new sentence.
UML Thumbnail

Using a Stable Sort with Custom Comparator

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...