|
| 1 | +package medium300; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class HighestCharacter { |
| 6 | + |
| 7 | + class Element implements Comparable<Element> { |
| 8 | + String sorted, original; |
| 9 | + |
| 10 | + public Element(String sorted, String original) { |
| 11 | + this.sorted = sorted; |
| 12 | + this.original = original; |
| 13 | + } |
| 14 | + |
| 15 | + public String getSorted() { |
| 16 | + return sorted; |
| 17 | + } |
| 18 | + |
| 19 | + public String getOriginal() { |
| 20 | + return original; |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + public int compareTo(Element o) { |
| 25 | + if (this.sorted.equals(o.sorted)) { |
| 26 | + if (this.original.equals(o.original)) { |
| 27 | + return 1; |
| 28 | + } else { |
| 29 | + return this.original.compareTo(o.original); |
| 30 | + } |
| 31 | + } else { |
| 32 | + return -this.sorted.compareTo(o.sorted); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public String highest(String[] strings) { |
| 38 | + |
| 39 | + List<Element> list = new ArrayList<>(); |
| 40 | + |
| 41 | + for (int i = 0; i < strings.length; i++) { |
| 42 | + char[] nz = strings[i].toCharArray(); |
| 43 | + Arrays.sort(nz); |
| 44 | + int n = nz.length; |
| 45 | + char t; |
| 46 | + for (int j = 0; j < n / 2; j++) { |
| 47 | + t = nz[j]; |
| 48 | + nz[j] = nz[n - j - 1]; |
| 49 | + nz[n - j - 1] = t; |
| 50 | + } |
| 51 | + list.add(new Element(new String(nz), strings[i])); |
| 52 | + } |
| 53 | + Collections.sort(list); |
| 54 | + // list.sort(Comparator.comparing(Element::getSorted).thenComparing(Element::getOriginal).reversed()); |
| 55 | + // for (Element e : list) System.out.println(e.original + " " + e.getSorted()); |
| 56 | + return list.get(0).original; |
| 57 | + } |
| 58 | + |
| 59 | + // public static void main(String[] args) { |
| 60 | + // HighestCharacter h = new HighestCharacter(); |
| 61 | + // System.out.println(h.highest(new String[]{"ab", "ba"})); |
| 62 | + // System.out.println(h.highest(new String[]{"a", "b", "d", "abxyz", |
| 63 | + // "abcxyz"})); |
| 64 | + // |
| 65 | + // } |
| 66 | +} |
0 commit comments