Skip to content

Commit 362aca7

Browse files
refactor 937
1 parent dd75347 commit 362aca7

File tree

1 file changed

+23
-50
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+23
-50
lines changed

src/main/java/com/fishercoder/solutions/_937.java

+23-50
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,30 @@
44
import java.util.List;
55
import java.util.TreeMap;
66

7-
/**
8-
* 937. Reorder Log Files
9-
*
10-
* You have an array of logs. Each log is a space delimited string of words.
11-
*
12-
* For each log, the first word in each log is an alphanumeric identifier. Then, either:
13-
*
14-
* Each word after the identifier will consist only of lowercase letters, or;
15-
* Each word after the identifier will consist only of digits.
16-
* We will call these two varieties of logs letter-logs and digit-logs. It is guaranteed that each log has at least one word after its identifier.
17-
*
18-
* Reorder the logs so that all of the letter-logs come before any digit-log.
19-
* The letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties.
20-
* The digit-logs should be put in their original order.
21-
*
22-
* Return the final order of the logs.
23-
*
24-
* Example 1:
25-
*
26-
* Input: ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]
27-
* Output: ["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]
28-
*
29-
* Note:
30-
* 0 <= logs.length <= 100
31-
* 3 <= logs[i].length <= 100
32-
* logs[i] is guaranteed to have an identifier, and a word after the identifier.
33-
*/
347
public class _937 {
35-
public static class Solution1 {
36-
public String[] reorderLogFiles(String[] logs) {
37-
TreeMap<String, String> letterLogMap = new TreeMap<>();
38-
List<String> digitLogList = new ArrayList<>();
39-
for (String log : logs) {
40-
int firstSpaceIndex = log.indexOf(' ');
41-
String id = log.substring(0, firstSpaceIndex);
42-
if (Character.isAlphabetic(log.charAt(firstSpaceIndex + 1))) {
43-
String key = log.substring(firstSpaceIndex + 1) + id;
44-
letterLogMap.put(key, log);
45-
} else {
46-
digitLogList.add(log);
8+
public static class Solution1 {
9+
public String[] reorderLogFiles(String[] logs) {
10+
TreeMap<String, String> letterLogMap = new TreeMap<>();
11+
List<String> digitLogList = new ArrayList<>();
12+
for (String log : logs) {
13+
int firstSpaceIndex = log.indexOf(' ');
14+
String id = log.substring(0, firstSpaceIndex);
15+
if (Character.isAlphabetic(log.charAt(firstSpaceIndex + 1))) {
16+
String key = log.substring(firstSpaceIndex + 1) + id;
17+
letterLogMap.put(key, log);
18+
} else {
19+
digitLogList.add(log);
20+
}
21+
}
22+
String[] reorderedLogs = new String[logs.length];
23+
int i = 0;
24+
for (String key : letterLogMap.keySet()) {
25+
reorderedLogs[i++] = letterLogMap.get(key);
26+
}
27+
for (String log : digitLogList) {
28+
reorderedLogs[i++] = log;
29+
}
30+
return reorderedLogs;
4731
}
48-
}
49-
String[] reorderedLogs = new String[logs.length];
50-
int i = 0;
51-
for (String key : letterLogMap.keySet()) {
52-
reorderedLogs[i++] = letterLogMap.get(key);
53-
}
54-
for (String log : digitLogList) {
55-
reorderedLogs[i++] = log;
56-
}
57-
return reorderedLogs;
5832
}
59-
}
6033
}

0 commit comments

Comments
 (0)