Reorder Data in Log Files
MediumSorting · Amazon
You are given a list of strings logs. Each log is a sequence of words separated by single spaces. The first word is the log's identifier; everything after the first space is its content.
There are two kinds of logs:
- Letter-log: every word of the content is made of lowercase letters.
- Digit-log: every word of the content is made of digits.
Return the logs reordered as follows:
- All letter-logs come before all digit-logs.
- Letter-logs are sorted by their content (compared as whole strings). If two letter-logs have identical content, the one with the smaller identifier comes first.
- Digit-logs keep the same relative order they had in the input.
Return the logs as complete, unchanged strings.
Example 1
- Input:
- logs = ["z3 9 0", "a2 cat dog", "b1 cat cow", "c7 4 4", "q1 cat dog"]
- Output:
- ["b1 cat cow", "a2 cat dog", "q1 cat dog", "z3 9 0", "c7 4 4"]
- Explanation
- The letter-logs sort by content:
cat cowcomes beforecat dog. Two logs share the contentcat dog, so identifiera2goes beforeq1. The digit-logsz3andc7follow in their original order.
Example 2
- Input:
- logs = ["k1 b a", "m2 b", "d1 07"]
- Output:
- ["m2 b", "k1 b a", "d1 07"]
- Explanation
- The content
bis a prefix ofb a, so it sorts first. The single digit-log goes last.
Example 3
- Input:
- logs = ["d4 3", "d2 2", "d9 5"]
- Output:
- ["d4 3", "d2 2", "d9 5"]
- Explanation
- Every log is a digit-log, so nothing moves.
Constraints
- 1 ≤
logs.length≤ 100 - 3 ≤
logs[i].length≤ 100 logs[i]contains only lowercase English letters, digits, and single spaces- Each log has an identifier followed by at least one content word, with no leading or trailing spaces