TwoWaysSorting
SRM 621 · 2013-12-22 · by boba5551
Problem Statement
Sasha has a
So far, Sasha has learned two ways of sorting strings:
- He can sort strings lexicographically. For example, "car" < "carriage" < "cats" < "doggies". (See Notes for a definition of the lexicographic order.)
- He can also sort strings according to their lengths in ascending order. For example, "car" < "cats" < "doggies" < "carriage".
Sasha now wonders whether stringList is sorted in either of these two ways. Return "lexicographically" (quotes for clarity) if stringList is sorted lexicographically but not according to the string lengths. Return "lengths" if stringList is sorted according to the string lengths but not lexicographically. Return "both" if it is sorted in both ways. Otherwise, return "none".
Notes
- String A is lexicographically smaller than string B if A contains a character with a smaller ASCII value in the first position where they differ. In case one of the strings ends before they have a different character, the shorter one is considered smaller.
Constraints
- stringList will contain between 1 and 50 elements, inclusive.
- Each element of stringList will contain between 1 and 50 characters, inclusive.
- Each character of each element of stringList will be a lowercase English letter ('a'-'z').
- Every two elements of stringList will have different lengths.
{"a", "aa", "bbb"}
Returns: "both"
These strings are sorted both lexicographically and according to their lengths.
{"c", "bb", "aaa"}
Returns: "lengths"
The lengths of these strings are in ascending order. However, they are not sorted lexicographically as, for instance, "aaa" is lexicographically smaller than "c".
{"etdfgfh", "aio"}
Returns: "none"
Here the first string is longer than the second one, so they are not sorted according to length. (Note that we require the lengths to be in ascending order.) Similarly, they are not sorted lexicographically: "aio" should come before "etdfgfh".
{"aaa", "z"}
Returns: "lexicographically"
The input strings are sorted lexicographically only.
{"z"}
Returns: "both"
Submissions are judged against all 50 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class TwoWaysSorting with a public method string sortingMethod(vector<string> stringList) · 50 test cases · 2 s / 256 MB per case