Sort letter-logs by content and keep digit-logs in input order

Quick Overview

Reorder a list of log strings so that letter-logs come first, sorted by their content with the identifier as a tie-break, while digit-logs keep their original order. It tests string parsing, custom sort keys and preserving relative order.

Sort letter-logs by content and keep digit-logs in input order

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a list of log strings `logs`. Each log has the format `"<identifier> <content>"`, where the identifier is a single word with no spaces and the content is everything after the first space. There are two kinds of logs: - Letter-logs: the content consists only of lowercase English letters and spaces. - Digit-logs: the content consists only of digits and spaces. Reorder the logs as follows: - All letter-logs come before all digit-logs. - Letter-logs are sorted lexicographically by their content. If two letter-logs have the same content, sort them lexicographically by their identifier. - Digit-logs keep their original relative order. Return the reordered list of log strings. ### Function Signature ```python def reorder_logs(logs: list[str]) -> list[str]: ``` ### Rules - The identifier is the text before the first space and the content is the text after it. Only the content decides whether a log is a letter-log or a digit-log. - Lexicographic order is plain character-by-character string comparison (the default comparison of Python strings). A space sorts before every letter, and a string sorts before any longer string it is a prefix of. - Return a list that contains every input log exactly once. ### Constraints - `1 <= len(logs) <= 100` - `3 <= len(logs[i]) <= 100` - All tokens of `logs[i]` are separated by a single space. - `logs[i]` is guaranteed to have an identifier and at least one word after the identifier. ### Examples ```text 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"] ``` The letter-logs sort by content as "art can" < "art zero" < "own kit dig". The digit-logs follow in the order they appeared, dig1 before dig2. ```text 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"] ``` Sorting the letter-logs by content gives "act car" < "act zoo" < "off key dog", so g1 comes before a8 even though its identifier is larger. The digit-logs a1 and zo4 keep their input order. ```text Input: logs = ["x9 hello world","d1 0 4","a2 hello world","b1 hello"] Output: ["b1 hello","a2 hello world","x9 hello world","d1 0 4"] ``` "hello" is a prefix of "hello world", so b1 comes first. The other two letter-logs have the same content, so the identifier breaks the tie and a2 comes before x9.

Overview: Reorder a list of log strings so that letter-logs come first, sorted by their content with the identifier as a tie-break, while digit-logs keep their original order. It tests string parsing, custom sort keys and preserving relative order.

Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of log strings logs. Each log has the format "<identifier> <content>", where the identifier is a single word with no spaces and the content is everything after the first space.

There are two kinds of logs:

  • Letter-logs: the content consists only of lowercase English letters and spaces.
  • Digit-logs: the content consists only of digits and spaces.

Reorder the logs as follows:

  • All letter-logs come before all digit-logs.
  • Letter-logs are sorted lexicographically by their content. If two letter-logs have the same content, sort them lexicographically by their identifier.
  • Digit-logs keep their original relative order.

Return the reordered list of log strings.

Function Signature

def reorder_logs(logs: list[str]) -> list[str]:

Rules

  • The identifier is the text before the first space and the content is the text after it. Only the content decides whether a log is a letter-log or a digit-log.
  • Lexicographic order is plain character-by-character string comparison (the default comparison of Python strings). A space sorts before every letter, and a string sorts before any longer string it is a prefix of.
  • Return a list that contains every input log exactly once.

Constraints

  • 1 <= len(logs) <= 100
  • 3 <= len(logs[i]) <= 100
  • All tokens of logs[i] are separated by a single space.
  • logs[i] is guaranteed to have an identifier and at least one word after the identifier.

Examples

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"]

The letter-logs sort by content as "art can" < "art zero" < "own kit dig". The digit-logs follow in the order they appeared, dig1 before dig2.

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"]

Sorting the letter-logs by content gives "act car" < "act zoo" < "off key dog", so g1 comes before a8 even though its identifier is larger. The digit-logs a1 and zo4 keep their input order.

Input: logs = ["x9 hello world","d1 0 4","a2 hello world","b1 hello"]
Output: ["b1 hello","a2 hello world","x9 hello world","d1 0 4"]

"hello" is a prefix of "hello world", so b1 comes first. The other two letter-logs have the same content, so the identifier breaks the tie and a2 comes before x9.