Skip to content

Commit 69890f5

Browse files
solves reorder data in log files
1 parent bfa43a4 commit 69890f5

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@
252252
| 925 | [Long Pressed Name](https://leetcode.com/problems/long-pressed-name) | [![Java](assets/java.png)](src/LongPressedName.java) |
253253
| 929 | [Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses) | [![Java](assets/java.png)](src/UniqueEmailAddresses.java) |
254254
| 933 | [Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls) | [![Java](assets/java.png)](src/NumberOfRecentCalls.java) |
255-
| 937 | [Reorder Data In Log Files](https://leetcode.com/problems/reorder-data-in-log-files) | |
255+
| 937 | [Reorder Data In Log Files](https://leetcode.com/problems/reorder-data-in-log-files) | [![Java](assets/java.png)](src/ReorderDataInLogFiles.java) |
256256
| 938 | [Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst) | |
257257
| 941 | [Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array) | |
258258
| 942 | [DI String Match](https://leetcode.com/problems/di-string-match) | |

src/ReorderDataInLogFiles.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.Arrays;
2+
3+
public class ReorderDataInLogFiles {
4+
public String[] reorderLogFiles(String[] logs) {
5+
Arrays.sort(logs, (log1, log2) -> {
6+
String[] split1 = log1.split(" ", 2);
7+
String[] split2 = log2.split(" ", 2);
8+
9+
boolean isDigit1 = Character.isDigit(split1[1].charAt(0));
10+
boolean isDigit2 = Character.isDigit(split2[1].charAt(0));
11+
12+
// case 1). both logs are letter-logs
13+
if (!isDigit1 && !isDigit2) {
14+
// first compare the content
15+
int cmp = split1[1].compareTo(split2[1]);
16+
if (cmp != 0) return cmp;
17+
// logs of same content, compare the identifiers
18+
return split1[0].compareTo(split2[0]);
19+
}
20+
21+
// case 2). one of logs is digit-log
22+
if (!isDigit1)
23+
// the letter-log comes before digit-logs
24+
return -1;
25+
else if (!isDigit2)
26+
return 1;
27+
else
28+
// case 3). both logs are digit-log
29+
return 0;
30+
});
31+
return logs;
32+
}
33+
}

0 commit comments

Comments
 (0)