'Coding has over 700 languages', '67% of programming jobs aren’t in the technology industry', 'Coding is behind almost everything that is powered by electricity'
Topics

Party Over

Easy
0/40
12 upvotes
Asked in companies
eBayWatchGuard Technologies

Problem statement

Ninja is coming after a long party to his home, but he faces a monster while returning. Monster puts up a condition to Ninja in order to free him. The monster gives him ‘n’ strings and asks him to sort them. However, he adds an extra condition to him.

Since the monster knows that Ninja could do it easily, the monster wants him to sort them using the last letter of each string. If there are strings with the same last character, sort them based on their second last character and so on.

Ninja gets totally confused, he asks you to solve the problem. Can you help Ninja figure out the correct order of strings?

Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= T <= 10
1 <= n <= 10^3
1 <= size of string <= 10^2

Where 'T’ is the number of test cases, ‘n’ denotes the number of strings

Time Limit: 1 sec

Sample Input 1:

2
5
abc abd aba xyb cdg
3
jog oop nop
Sample Output 1:
aba xyb abc abd cdg
jog nop oop
Explanation for Sample Input 1:
In the first test case, the given strings are [“abc”,”abd”,”xyb”,”cdg”], the last character of strings are: ‘c’,’d’,’a’,’b’,’g’. So their sorted order is: ‘a’,’b’,’c’,’d’,’g’. Hence the sorted order of strings are:“aba”,”xyb”,”abc”,”abd”,”cdg”.

In the second test case, the given strings are [“jog”,”nop”,”oop”] the last characters of strings are: ‘g’,’p’,’p’.So their sorted order is : ‘g’,’p’,’p’. As we can see for strings “oop” and “nop”, their last character matches; we need to check from the second previous character till we find a mismatched character and sort them accordingly. Hence the sorted order of strings are: “jog”,” nop”,” oop”.

Sample Input 2:

2
4
truck bus car auto
3
teacher student headmaster
Sample Output 2:
truck auto car bus
teacher headmaster student
Explanation for Sample Input 2:
In the first test case, the given strings are [“truck”,”bus”,”car”,”auto”], the last character of strings are: ‘k’,’s’,’r’,’o’.So their sorted order is: ‘k’,’o’,’r’,’s’’. Hence the sorted order of strings are:“truck”,”auto”,”car”,”bus”.

In the second test case, the given strings are [“teacher”,”student”,”headmaster”] the last characters of strings are: ‘r’,’t’,’r’.So their sorted order is : ‘r’,’r’,’t’. As we can see for strings “teacher” and “headmaster”, their last character matches; we need to check from the second previous character till we find a mismatched character and sort them accordingly. Hence the sorted order of strings are: “teacher”,” headmaster”,”student”.
Full screen
Console