Reorder data in log files

September 13, 2022

array-and-hashmap

Problem URL: Reorder data in log files

We will create two different array, one for the letter logs and another for the digits logs. Then we iterate over all the logs and separate them. Then we sort the letters log, first by the content, and if the content are the same then by the key. Finally we merge both letter logs and digit logs and return it as the result.

class Solution:
    def reorderLogFiles(self, logs: List[str]) -> List[str]:
        letters, digits = [], []

        for log in logs:
            if log[-1].isdigit():
                digits.append(log)
            else:
                letters.append(log)

        letters.sort(key=(lambda x: (x.split()[1:], x.split()[0])))

        return letters + digits

Time Complexity: O(nlog(n))
Space Complexity: O(n)