Skip to content

Commit b7da55c

Browse files
add 937
1 parent 0fed6ce commit b7da55c

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Your ideas/fixes/algorithms are more than welcome!
4141
|944|[Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_944.java) | O(n) | O(1) | |Easy|
4242
|941|[Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_941.java) | O(n) | O(1) | |Easy|
4343
|938|[Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_938.java) | O(n) | O(n) | |Medium| BST, recursion, DFS
44+
|937|[Reorder Log Files](https://leetcode.com/problems/reorder-log-files/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_937.java) | O(n) | O(n) | |Easy|
4445
|933|[Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_933.java) | O(n) | O(n) | |Easy|
4546
|929|[Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_929.java) | O(n) | O(n) | |Easy|
4647
|925|[Long Pressed Name](https://leetcode.com/problems/long-pressed-name/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_925.java) | O(n) | O(1) | |Easy|
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.TreeMap;
6+
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+
*/
34+
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);
47+
}
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;
58+
}
59+
}
60+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._937;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _937Test {
10+
private static _937.Solution1 solution1;
11+
private static String[] logs;
12+
private static String[] expected;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _937.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
logs = new String[] {"a1 9 2 3 1", "g1 act car", "zo4 4 7", "ab1 off key dog", "a8 act zoo"};
22+
expected =
23+
new String[] {"g1 act car", "a8 act zoo", "ab1 off key dog", "a1 9 2 3 1", "zo4 4 7"};
24+
assertArrayEquals(expected, solution1.reorderLogFiles(logs));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
logs = new String[] {"t kvr", "r 3 1", "i 403", "7 so", "t 54"};
30+
expected = new String[] {"t kvr", "7 so", "r 3 1", "i 403", "t 54"};
31+
assertArrayEquals(expected, solution1.reorderLogFiles(logs));
32+
}
33+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy