-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReorderDataInLogFiles.java
More file actions
31 lines (28 loc) · 1.14 KB
/
Copy pathReorderDataInLogFiles.java
File metadata and controls
31 lines (28 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//time:O(nlgn)
//space:O(n)
class Solution {
public String[] reorderLogFiles(String[] logs) {
Arrays.sort(logs, (s1, s2) -> { // Array.sort(logs,0,5) index 0 included,index 5 excluded.
String[] split1 = s1.split(" ", 2); // split by " ".
String[] split2 = s2.split(" ", 2);
boolean isDigit1 = Character.isDigit(split1[1].charAt(0));
boolean isDigit2 = Character.isDigit(split2[1].charAt(0));
if(!isDigit1 && !isDigit2) {
// both letter-logs.
int comp = split1[1].compareTo(split2[1]);
if (comp == 0) return split1[0].compareTo(split2[0]);
else return comp;
} else if (isDigit1 && isDigit2) {
// both digit-logs. So keep them in original order
return 0;
} else if (isDigit1 && !isDigit2) {
// first is digit, second is letter. bring letter to forward.
return 1;
} else {
//first is letter, second is digit. keep them in this order.
return -1;
}
});
return logs;
}
}