[Leetcode]937 : Reorder Data in Log Files

Link : https://leetcode.com/problems/reorder-data-in-log-files/

Problem :
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.

Thoughts :
0. 分兩個 array 存。一個存 letter、一個存數字
1. 確認當前的 log 是 letter 還是數字。
2. 如果是 letter 存到 letter 陣列,如果是非 letter 存到nums陣列。
3. letter 的 array 做 sort、nums的按照原本的排序就好。

Solution :

time complexity = O(nlog n), space complexity = O(n)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store