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:

Letter-logs: All words after the identifier contain only lowercase English letters
Digit-logs: All words after the identifier contain only digits

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.

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
๐Ÿ“š Company Filing System Organization๐Ÿ“„ Research Papers (Letter-logs)let1: Art Researchlet3: Art Theorylet2: Biology Studyโœ“ Sorted alphabetically:1. "Art Research" < "Art Theory"2. "Biology Study" comes last๐Ÿ“Š Financial Records (Digit-logs)dig1: Q1 Reportdig2: Q2 Reportโฐ Chronological order maintained๐Ÿ“… Q1 โ†’ Q2 (Original sequence)๐Ÿ”’ Audit compliance requires no reordering๐Ÿ—ƒ๏ธ Final Organized Filing SystemArt ResearchArt TheoryBiology StudyQ1 ReportQ2 ReportResearch Papers (Alphabetical) โ†’ Financial Records (Chronological)Custom Sorting Algorithm: O(n log n) Time Complexity๐ŸŽฏ Efficient & Organized Filing System
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.
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 18
89.2K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen