Random Words
Practice
2 (6 votes)
Sorting
Implementation
Recruit
Approved
Easy
Approved
Problem
46% Success 4100 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Given N words \(a_i\) of lowercase letters. Arrange these words such that word \(a_i\) of length \(l_i\) comes before word \(a_j\) of length \(l_j\) if

1) \(l_i < l_j\).
OR
2) \(l_i = l_j\) and the first different letter in \(a_i\) < corresponding letter in \(a_j\). Letters are compared with respect to their ASCII Value.

For example: \(a_i\) = "arm", \(a_j\) = "ant", so "ant" comes before "arm" because second letter in \(a_j\) n < second letter in \(a_i\) r.

Input:

In the first line: Given an integer N.
In the second line: N strings \(a_i\) follow.

Output:

Print the words in the order illustrated in the question.

Constraints:

\(1 \le N \le 100\).
\(1 \le l_i \le 10\).

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
Points:50
6 votes
Tags:
Counting and ArrangementsDynamic ProgrammingAlgorithmsMedium-Hard
Points:30
Tags:
Data StructuresMediumApprovedSegment treeData Structures