MCPcopy Create free account
hub / github.com/Rohit91singh9/Coding-DP-DSA / Result

Class Result

Result.java:6–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4import java.util.List;
5
6class Result {
7 public static List<String> sortBoxes(List<String> boxList) {
8 Comparator<String> comparator = new Comparator<String>() {
9 @Override
10 public int compare(String box1, String box2) {
11 // split each junction box into two parts identifier, content
12 String[] split1 = box1.split(" ", 2);
13 String[] split2 = box2.split(" ", 2);
14
15 boolean isDigit1 = Character.isDigit(split1[1].charAt(0));
16 boolean isDigit2 = Character.isDigit(split2[1].charAt(0));
17
18 // case 1). both boxes are letter-boxes
19 if (!isDigit1 && !isDigit2) {
20 // first compare the content
21 int cmp = split1[1].compareTo(split2[1]);
22 if (cmp != 0)
23 return cmp;
24 // boxes of same content, compare the identifiers
25 return split1[0].compareTo(split2[0]);
26 }
27 // case 2). one of boxes is digit
28 if (!isDigit1 && isDigit2)
29 // the letter box comes before digit-boxes
30 return -1;
31 else if (isDigit1 && !isDigit2)
32 return 1;
33 else
34 // case 3). both boxes are digit
35 return 0;
36 }
37 };
38
39 Collections.sort(boxList, comparator);
40 return boxList;
41 }
42 public static void main(String[] args) {
43 List<String> list = new ArrayList<String>();
44 list.add("ykc 82 01");
45 list.add("eo first qpx");
46 list.add("09z cat hamster");
47 list.add("06f 12 25 6");
48 list.add("az0 first qpx");
49 list.add("236 cat dog rabbit snake");
50 System.out.println(sortBoxes(list));
51 }
52
53}
54
55/*
56NOTE:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected