Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Sentence Sort
EASY
9 upvotes
Strings
Sorting
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Strings
-
-
Sorting
-
-
Other topics
Problem solved
Badge
Skill meter
Matrices (2D Arrays)
-
-
Linked List
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Sentence Sort

Contributed by
Paritosh Singh
Easy
yellow-spark
0/40
Share
9 upvotes

Problem Statement

You are given a sentence in the form of a string ‘str’ containing no more than 9 words, each word in the sentence is suffixed by a unique number ranging from 1 to N, where N is the number of words in the sentence. Your task is to reorder the words in ‘str’ according to the suffix number of each word and return it as the string.

For Example:
You are given ‘str’ = ‘A1 person3 good2’, in this we can see the ordering of the words like ‘A’,  ‘good’ ‘person’ according to the suffix number. Hence the answer string is ‘A good person’.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
2
A1 person3 good2
world2 hello1
Sample Input 2:
A good person
hello world
Explanation for input 1:
For the first test case, ‘str’ = ‘A1 person3 good2’, in this we can see the ordering of the words like ‘A’,  ‘good’ ‘person’ according to the suffix number. Hence the answer string is ‘A good person’.

For the second test case, ‘str’ = ‘world2 hello1’, in this we can see the ordering of the words like ‘ hello’, and ‘world’, according to the suffix number Hence the answer string is ‘hello world’.
Sample Input 2:
2
Code1 studio2 coding3 ninjas4
hi1
Sample Output 2:
Code studio coding ninjas
hi 
Reset Code
Full screen
Auto
copy-code
Console