Rearrange Words in a Sentence

You are provided with a sentence 'TEXT'. Each word in the sentence is separated by a single space, and the sentence starts with a capital letter. Your task is to rearrange the words in 'TEXT' based on increasing order of their length.

Example:

Input:
T = 1
TEXT = "Hello World from OpenAI"
Output:
"AI from World Hello Open"
Explanation:

The words are rearranged based on their lengths: "AI" (2), "from" (4), "World" (5), "Hello" (5), "Open" (4). Since "Hello" and "World" have the same length, they are arranged based on their original order.

Constraints:

  • 1 <= T <= 5
  • 1 <= |TEXT| <= 105
  • Where | 'TEXT' | denotes the length of the 'TEXT' sentence.
Note:
If two words have the same length, they remain in their original order as in 'TEXT'. 
AnswerBot
3d

Rearrange words in a sentence based on increasing order of their length.

  • Split the sentence into words and calculate the length of each word.

  • Sort the words based on their lengths in increasing order.

  • If...read more

Help your peers!
Add answer anonymously...
JPMorgan Chase & Co. Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter