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.
This approach involves separating the logs into letter-logs and digit-logs. We then use a custom sorting function to sort the letter-logs based on their content first and their identifiers second, before finally appending the digit-logs to the sorted letter-logs.
This solution defines a function which separates the logs into letter-logs and digit-logs. It uses Python's built-in sort functionality with a custom sorting key. The key first compares everything after the identifier, and then by identifier itself.
Python
Java
JavaScript
Time complexity is O(M*N*logN), where N is the number of logs and M is the maximum length of a single log. Sorting the letter logs is the most time-consuming operation.
Space complexity is O(N), for storing the letter-logs and digit-logs separately.
This approach intends to utilize a two-pass operation, where in the first pass it processes and categorizes letter-logs and digit-logs, and in the second pass it performs an in-place sort on the letter logs. Lastly, it reconstructs the original array by appending digit-logs after the sorted letter-logs.
The solution in C utilizes two pointers to distinguish between digit and letter logs and uses qsort for in-place sorting of letter logs. The custom compare function accurately handles comparison based on the directive content and identifier.
Time complexity is O(M*N*logN) due to the sorting of letter-logs using qsort.
Space complexity is O(1), because sorting and operations are done in place.
We can use a custom sorting method to divide the logs into two categories: letter logs and digit logs.
For letter logs, we need to sort them according to the problem requirements, i.e., first by content and then by identifier.
For digit logs, we only need to maintain their original relative order.
The time complexity is O(n times log n), and the space complexity is O(n). Where n is the number of logs.
| Approach | Complexity |
|---|---|
| Custom Sorting Using Comparator | Time complexity is O(M*N*logN), where N is the number of logs and M is the maximum length of a single log. Sorting the letter logs is the most time-consuming operation. |
| Two-Pass Solution with In-place Operations | Time complexity is O(M*N*logN) due to the sorting of letter-logs using qsort. |
| Custom Sorting | — |
| 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. |
Reorder Data in Log Files | coding interview question amazon | leetcode 937 • thecodingworld • 26,652 views views
Watch 9 more video solutions →Practice Reorder Data in Log Files with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor