Problem Description
Reorder an array of logs so that letter-logs come before digit-logs. Letter-logs are ordered lexicographically by content and, if tied, by identifier; digit-logs maintain their original order.
Key Insights
- Separate the logs into letter-logs and digit-logs.
- Determine the type of log by checking if the first character of the log's content is a digit.
- Sort letter-logs by their content and use identifiers as a secondary key.
- Retain the relative order of digit-logs using a stable approach.
Space and Time Complexity
Time Complexity: O(N log N) for sorting the letter-logs, where N is the number of logs. Space Complexity: O(N) for storing the letter and digit logs separately.
Solution
The solution involves splitting each log into an identifier and the rest of the content. For each log, check if the content starts with a digit to classify it as a digit-log; otherwise, classify it as a letter-log. Then, sort the letter-logs lexicographically by content and identifier. Finally, concatenate the sorted letter-logs with the original-order digit-logs to produce the final ordering.