Reorder Data in Log Files

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: - **Letter-logs**: all words (except the identifier) consist of lowercase English letters. - **Digit-logs**: all words (except the identifier) consist of digits. Reorder these logs so that: 1. The letter-logs come before all digit-logs. 2. The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers. 3. The digit-logs maintain their relative ordering. Return the final order of the logs.

30:00

Reorder Data in Log Files
medium
Free written solution
Topics
Companies

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:

  • Letter-logs: all words (except the identifier) consist of lowercase English letters.
  • Digit-logs: all words (except the identifier) consist of digits.

Reorder these logs so that:

  1. The letter-logs come before all digit-logs.
  2. The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
  3. The digit-logs maintain their relative ordering.

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"]
Constraints:
  • 1≤logs.length≤1001 \leq \text{logs.length} \leq 100

  • 3≤logs[i].length≤1003 \leq \text{logs}[i]\text{.length} \leq 100

  • All the tokens of logs[i]\text{logs}[i] are separated by a single space.

  • logs[i]\text{logs}[i] is guaranteed to have an identifier and at least one word after the identifier.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"logs":["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]}

Each log is an identifier followed by its content. The first character of the content tells letter-logs from digit-logs.

input logs · 5 still to route

dig18 1 5 1
let1art can
dig23 6
let2own kit dig
let3art zero

letter-logs · 0 · order not final

empty

digit-logs · 0 · input order kept

empty

Variables
VariableValue
logs5
DepthFunction Call
Stack empty
0/15