Reorder Data in Log Files - Problem
Reorder Data in Log Files is a classic string manipulation and sorting problem that simulates organizing server logs. You're given an array of log entries where each log is a space-delimited string. The first word is always the identifier, and the remaining words determine the log type:
Your task is to reorder these logs according to specific rules:
1. All letter-logs must come before digit-logs
2. Letter-logs are sorted lexicographically by their content (words after identifier)
3. If content is identical, sort by identifier lexicographically
4. Digit-logs maintain their original relative order
This problem tests your understanding of custom sorting and stable sorting algorithms.
Letter-logs: All words after the identifier contain only lowercase English lettersDigit-logs: All words after the identifier contain only digitsYour task is to reorder these logs according to specific rules:
1. All letter-logs must come before digit-logs
2. Letter-logs are sorted lexicographically by their content (words after identifier)
3. If content is identical, sort by identifier lexicographically
4. Digit-logs maintain their original relative order
This problem tests your understanding of custom sorting and stable sorting algorithms.
Input & Output
example_1.py โ Basic Mixed Logs
$
Input:
logs = ["dig1 8 1 5 1", "let1 art can", "dig2 3 6", "let2 own kit dig", "let3 art zero"]
โบ
Output:
["let1 art can", "let3 art zero", "let2 own kit dig", "dig1 8 1 5 1", "dig2 3 6"]
๐ก Note:
Letter-logs are sorted: "art can" comes before "art zero" which comes before "own kit dig". Since "let1 art can" and "let3 art zero" both start with "art", we compare the full content: "art can" < "art zero". Digit-logs maintain their original relative order.
example_2.py โ Identical Content
$
Input:
logs = ["a1 9 2 3 1", "g1 act car", "zo4 4 7", "ab1 off key dog", "a8 act zoo", "a2 act car"]
โบ
Output:
["a2 act car", "g1 act car", "a8 act zoo", "ab1 off key dog", "a1 9 2 3 1", "zo4 4 7"]
๐ก Note:
Letter-logs with identical content "act car" are sorted by identifier: "a2" comes before "g1". All letter-logs come before digit-logs which maintain their original order.
example_3.py โ Edge Case
$
Input:
logs = ["1 n u", "r 527", "j 893", "6 14", "6 82"]
โบ
Output:
["1 n u", "r 527", "j 893", "6 14", "6 82"]
๐ก Note:
Only "1 n u" is a letter-log (contains letters), so it comes first. All others are digit-logs and maintain their original relative order.
Constraints
- 1 โค logs.length โค 100
- 3 โค logs[i].length โค 100
- All the tokens of logs[i] are separated by a single space
- logs[i] is guaranteed to have an identifier and at least one word after the identifier
- The identifier consists of lowercase English letters and digits
- Letter-logs contain only lowercase English letters in the content
- Digit-logs contain only digits in the content
Visualization
Tap to expand
Understanding the Visualization
1
Identify Document Types
Separate research papers (letter-logs) from financial records (digit-logs) based on content type
2
Organize Research Papers
Sort research papers alphabetically by subject, then by document ID if subjects match
3
Maintain Financial Order
Keep financial records in their original chronological sequence for audit compliance
4
Combine Filing System
Place all organized research papers first, followed by financial records in original order
Key Takeaway
๐ฏ Key Insight: Custom sorting with comparators allows us to handle complex ordering requirements efficiently. By treating different data types with distinct sorting rules while maintaining stability for unchanged elements, we achieve optimal organization in O(n log n) time.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code