Watch 10 video solutions for Reorder Data in Log Files, a medium level problem involving Array, String, Sorting. This walkthrough by thecodingworld has 26,652 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier.
There are two types of logs:
Reorder these logs so that:
Return the final order of the logs.
Example 1:
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"] Explanation: The letter-log contents are all different, so their ordering is "art can", "art zero", "own kit dig". The digit-logs have a relative order of "dig1 8 1 5 1", "dig2 3 6".
Example 2:
Input: logs = ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"] Output: ["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]
Constraints:
1 <= logs.length <= 1003 <= logs[i].length <= 100logs[i] are separated by a single space.logs[i] is guaranteed to have an identifier and at least one word after the identifier.Problem Overview: You receive an array of log strings where each log starts with an identifier followed by words. Some logs contain letters (letter-logs) while others contain digits (digit-logs). The task is to reorder the logs so all letter-logs come before digit-logs. Letter-logs are sorted lexicographically by content, and if contents match, by identifier. Digit-logs keep their original order.
Approach 1: Custom Sorting Using Comparator (O(n log n) time, O(n) space)
This approach treats the problem as a specialized sorting task. Each log is parsed into two parts using a split operation: the identifier and the content. A custom comparator determines ordering rules. If both logs are letter-logs, compare their contents first; if contents match, compare identifiers. If one is a digit-log and the other is a letter-log, the letter-log always comes first. If both are digit-logs, maintain their original order by treating them as equal in sorting priority.
The key insight is encoding the ordering rules inside the comparator instead of manually rearranging elements. Most languages allow custom sort keys or comparator functions, making the implementation concise. This approach is commonly implemented with built-in sort() utilities and works well for problems involving structured ordering with multiple conditions. Time complexity is O(n log n) due to sorting, and space complexity is O(n) depending on the sorting implementation.
This technique relies heavily on understanding sorting behavior and string parsing with string operations.
Approach 2: Two-Pass Solution with In-place Operations (O(n log n) time, O(1) extra space)
This method separates the problem into two clear phases. First pass: iterate through the array and partition logs into letter-logs and digit-logs. Since digit-logs must preserve their relative order, they are simply collected in sequence. Letter-logs are stored in the front portion of the array or a temporary list. Second pass: sort only the letter-logs based on their content and identifier.
After sorting the letter-logs, append the digit-logs back in their original order. Because digit-logs are never reordered internally, their stability is preserved automatically. This approach avoids unnecessary comparisons between digit-logs and focuses sorting only on the relevant subset.
The time complexity remains O(n log n) due to sorting the letter-logs. However, practical performance improves when many logs are digits because fewer elements are sorted. Space complexity can be reduced to O(1) if the partitioning is done in-place within the array. This solution demonstrates careful manipulation of arrays and stable ordering constraints.
Recommended for interviews: The custom comparator approach is what most interviewers expect. It directly models the ordering rules in code and is easy to reason about during discussion. The two-pass strategy shows deeper control over array manipulation and stability but is usually longer to implement. Mentioning both demonstrates understanding of sorting mechanics and trade-offs.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Custom Sorting Using Comparator | O(n log n) | O(n) | General case. Clean implementation using built-in sorting with custom comparison rules. |
| Two-Pass Partition + Sort Letter Logs | O(n log n) | O(1) | Useful when minimizing extra memory or when digit-logs dominate the input. |