-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path937.py
32 lines (22 loc) · 827 Bytes
/
937.py
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
# Name: Reorder Data in Log Files
# Link: https://leetcode.com/problems/reorder-data-in-log-files/
# Method: Sorting after splitting based on digit presence
# Time: O(n\*log(n))
# Space: O(n)
# Difficulty: Easy
from typing import List, Tuple
class Solution:
def reorderLogFiles(self, logs: List[str]) -> List[str]:
ltr_logs = []
digit_logs = []
for log in logs:
_, log_data = log.split(' ', 1)
if all(c == " " or c.isdigit() for c in log_data):
digit_logs.append(log)
else:
ltr_logs.append(log)
def get_log_comparator_val(log:str) -> Tuple[str, str]:
log_id , log_data = log.split(" ", 1)
return (log_data, log_id)
ltr_logs.sort(key=get_log_comparator_val)
return ltr_logs + digit_logs