Reorder Data in Log Files (LeetCode 937): Solution and Explanation

Medium
Array
String
Sorting

Put letter-logs first, sorted by content and then identifier, followed by digit-logs in their original order.

Intuition

Only one character decides a log's type: the first character after the identifier. If it is a digit the whole log is a digit-log.

Digit-logs never move relative to each other, so they can go straight into a list in input order.

Letter-logs need a two-level order. Sorting by the pair (content, identifier) handles both rules at once: content decides first, and the identifier only breaks ties.

Approach: Partition, then sort by a two-part key

  1. Split each log at its first space into identifier and content.
  2. If the content starts with a digit, append the log to the digit list.
  3. Otherwise append it to the letter list.
  4. Sort the letter list by content, breaking ties by identifier.
  5. Return the letter list followed by the digit list.

Step-by-step example

logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]
  1. dig1 and dig2 have contents starting with 8 and 3, so they go to the digit list in that order.
  2. let1, let2 and let3 go to the letter list.
  3. Sorting by content gives "art can" (let1), "art zero" (let3), "own kit dig" (let2).
  4. The result is let1, let3, let2, then dig1, dig2.

Code: Python, Java, C++

1class Solution:
2    def reorderLogFiles(self, logs: List[str]) -> List[str]:
3        letters, digits = [], []
4        for log in logs:
5            ident, rest = log.split(" ", 1)
6            if rest[0].isdigit():
7                digits.append(log)
8            else:
9                letters.append((rest, ident, log))
10        letters.sort()
11        return [log for _, _, log in letters] + digits
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(M n log n)

Sorting does O(n log n) comparisons and each string comparison costs up to O(M) for logs of length M.

Space

O(M n)

The split contents and the output list hold every log.

Other approaches

Single stable sort with a custom key (time O(M n log n), space O(M n))

Sort all logs with one key: letter-logs get (0, content, identifier) and digit-logs get (1,). A stable sort keeps digit-logs in input order.

Custom comparator over the whole array (time O(M n log n), space O(n))

Write a comparator that ranks letter-logs before digit-logs, compares letter-logs by content then identifier, and returns 0 for two digit-logs. It needs a stable sort to keep digit-log order.

Common mistakes

  • Testing the type with the last character or the identifier. Identifiers can contain digits; only the content decides the type.
  • Sorting digit-logs or using an unstable sort on the whole array, which can shuffle digit-logs.
  • Comparing only the first word of the content. Contents are compared as whole strings, spaces included.
  • Forgetting the identifier tie-break when two letter-logs have the same content.

Frequently asked questions

Why split only at the first space?

The identifier is exactly the first word. Everything after the first space is the content and must be compared as one string.

Do I need a stable sort?

Not for this approach: digit-logs are never sorted, and letter-logs have unique (content, identifier) keys in practice. A single combined sort does need stability.

What counts as lexicographic order here?

Plain character-by-character comparison, as in Python string comparison or ordinal comparison in C#.