Interview Questions/Coding/Reorder Sentence By Word Length

Reorder Sentence By Word Length

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Reorder Sentence By Word Length

A sentence starts with an uppercase letter and ends with a period. Rearrange its words in increasing order of word length. If two words have the same length, keep their original relative order. After rearranging, rebuild the sentence so only the first character is uppercase, every other letter is lowercase, words are separated by single spaces, and the sentence ends with a period.

Examples

Example 1
Input:
Practers is cool.
Output:
Is cool Practers.

Explanation: The words are reordered by lengths 2, 4, and 8.

Approach hint

Remove the final period before sorting the words.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp