Problem Statement
As some of you may know, there is no name better than JOHN. Let's define the rules for comparing names. Each letter has a weight ('A' - 1, 'B' - 2, ..., 'Z' - 26). The weight of a name is the sum of the weights of all its letters. For example, the name MARK has weight 13 + 1 + 18 + 11 = 43.
When comparing two names, the one with the larger weight is considered better. In case of a tie, the one that comes earlier lexicographically is better. But there is one exception - the name JOHN is the best name of all.
You are given a String[] names, each element of which contains a single name. Sort the names from best to worst and return the sorted String[].
Definition
- Class:
- TheBestName
- Method:
- sort
- Parameters:
- String[]
- Returns:
- String[]
- Method signature:
- String[] sort(String[] names)
- (be sure your method is public)
Constraints
- names will contain between 1 and 50 elements, inclusive.
- Each element of names will contain between 1 and 50 characters, inclusive.
- Each element of names will contain only uppercase letters ('A'-'Z').
Examples
{"JOHN", "PETR", "ACRUSH"}Returns: {"JOHN", "ACRUSH", "PETR" }PETR has weight 59, ACRUSH has weight 70 and JOHN has a weight of only 47. But nevertheless JOHN is the best name, ACRUSH takes second place and PETR is the last.{"GLUK", "MARGARITKA"}Returns: {"MARGARITKA", "GLUK" }MARGARITKA is definitely better than GLUK.{"JOHN", "A", "AA", "AAA", "JOHN", "B", "BB", "BBB", "JOHN", "C", "CC", "CCC", "JOHN"}Returns: {"JOHN", "JOHN", "JOHN", "JOHN", "CCC", "BBB", "CC", "BB", "AAA", "C", "AA", "B", "A" }AA and B both have the same weight, but AA is better as it comes earlier lexicographically. For the same reason, AAA is better than C and BBB is better than CC.{"BATMAN", "SUPERMAN", "SPIDERMAN", "TERMINATOR"}Returns: {"TERMINATOR", "SUPERMAN", "SPIDERMAN", "BATMAN" }Here are some superheroes sorted by their names.