forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_937.java
33 lines (31 loc) · 1.15 KB
/
_937.java
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
32
33
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
public class _937 {
public static class Solution1 {
public String[] reorderLogFiles(String[] logs) {
TreeMap<String, String> letterLogMap = new TreeMap<>();
List<String> digitLogList = new ArrayList<>();
for (String log : logs) {
int firstSpaceIndex = log.indexOf(' ');
String id = log.substring(0, firstSpaceIndex);
if (Character.isAlphabetic(log.charAt(firstSpaceIndex + 1))) {
String key = log.substring(firstSpaceIndex + 1) + id;
letterLogMap.put(key, log);
} else {
digitLogList.add(log);
}
}
String[] reorderedLogs = new String[logs.length];
int i = 0;
for (String key : letterLogMap.keySet()) {
reorderedLogs[i++] = letterLogMap.get(key);
}
for (String log : digitLogList) {
reorderedLogs[i++] = log;
}
return reorderedLogs;
}
}
}