Rearrange words in a sentence

You are given a sentence 'TEXT'. Each word of 'TEXT' is separated by a single space and the first letter of 'TEXT' is capital. You need to rearrange the words of Text in increasing order of their length.

Note:
If two words have the same length then the word which comes first in 'TEXT' also comes first in the rearranged sentence.
Input format:
The first line of input contains an integer 'T' denoting the number of test cases. 

The first line of each test case contains a single sentence 'TEXT'.
Output Format:
For each test case, return the rearranged sentence. 
Constraints:
1 <= T <= 5
1 <= | TEXT | <= 10^5

Where | 'TEXT' | denotes the length of the 'TEXT' sentence.

Time limit: 1 sec
CodingNinjas
author
2y

Step 1 : Remove the capital letter (first letter of the sentence) and the full stop at the end.
Step 2 : I made use of the C++ STL library. I made a pair of strings and int where the string is the word...read more

CodingNinjas
author
2y
Using Sorting

The idea is to create an array of pairs, ‘WORD’, of size ‘N’. Where the first element of pair is word length and the second element is the starting index of word.

Now, start iterating ove...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
Get AmbitionBox app

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