Leetcode 937. Reorder Data in Log Files

Gary Chiang
3 min readApr 28, 2021

--

[Easy][Amazon]

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"]
Explanation:
The letter-log contents are all different, so their ordering is "art can", "art zero", "own kit dig".
The digit-logs have a relative order of "dig1 8 1 5 1", "dig2 3 6".

Example 2:

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

Constraints:

  • 1 <= logs.length <= 100
  • 3 <= logs[i].length <= 100
  • All the 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.

[Gary thoughts]

Test points

  1. understand the question asking — know how to sort
  2. use Comparator — know how to sort and reorder
  3. string functions — indexOf(“ ”), charAt(index), substring(startIndex), substring(startIndex, endIndex)
  4. ACS table, 0–9 and A to Z and a to z

Think

  1. always compare two string, keep sorting until we have all sorted
  2. there are four possible compare scenario, digit — digt, digit — char, char — digit, char — char.
  3. digit — digit (keep the same order) return 0; digit-char (need to swap the order) return 1; char — digit (keep the same order) return -1.
  4. char — char: need to sort as content dictionary order; we want to put “a” in the begining, so if string A bigger than string B, we will swap them; on the other hand, we will keep same format; If content are the same, we will sort by ID word by dictionary order.

[Code Structure]

comparator

identify string index and first char

if digit-digit -> return 0;

if digit-char -> return 1; (swap)

if char-digit -> return -1;

if char-char -> content A vs B -> return -1;

if char-char -> content B vs A -> return 1; (swap)

if char-char -> content A=A -> compare ID A vs ID B -> return -1;

if char-char -> content A=A -> compare ID Bvs ID A -> return 1; (swap)

Arrays.sort and return the reorder log

[Second Java]

Same method but different approach.

use split to seperate ID and content.

--

--

Gary Chiang
Gary Chiang

Written by Gary Chiang

CS new grad, 6 years experience related to supply chain management. Located in Bay area

No responses yet