Similar Problems

Similar Problems not available

Reorder Data In Log Files - Leetcode Solution

Companies:

LeetCode:  Reorder Data In Log Files Leetcode Solution

Difficulty: Medium

Topics: string sorting array  

Problem Statement: You are given an array of logs. Each log is a space-delimited string of words, where the first word is an alphanumeric identifier. There are two types of logs:

  1. Letter-logs: All words (except the identifier) consist of lowercase English letters.
  2. 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 by their identifiers.
  3. The digit-logs maintain their relative ordering.

Return the final order of the logs.

Solution: We can solve this problem by using a custom comparator to reorder the logs. First, we will separate the letter-logs and digit-logs into two separate arrays. Then we will apply a custom sorting algorithm to the letter-logs.

To create a custom sorting algorithm, we need to pass a Comparator object to the Arrays.Sort method. This object has a compare method that we can override according to our requirements. In this case, we want to sort the letter-logs by their contents first. If the contents are the same, then we will sort them by their identifiers.

Once we have sorted the letter-logs, we can concatenate them with the digit-logs and return the final array.

Here is the implementation using Java:

class Solution {
    public String[] reorderLogFiles(String[] logs) {
        //Separate the letter-logs and digit-logs into two separate arrays
        List<String> letterLogs = new ArrayList<>();
        List<String> digitLogs = new ArrayList<>();
        for(String log: logs){
            String[] words = log.split(" ");
            if(Character.isDigit(words[1].charAt(0))){
                digitLogs.add(log);
            }
            else{
                letterLogs.add(log);
            }
        }
        
        //Apply a custom sorting algorithm to the letter-logs
        Collections.sort(letterLogs, new Comparator<String>(){
           public int compare(String log1, String log2){
               String[] words1 = log1.split(" ", 2);
               String[] words2 = log2.split(" ", 2);
               int cmp = words1[1].compareTo(words2[1]);
               
               //If the contents are the same, then sort them by their identifiers
               if(cmp==0){
                   return words1[0].compareTo(words2[0]);
               }
               
               return cmp;
           } 
        });
        
        //Concatenate the letter-logs with the digit-logs and return the final array
        String[] result = new String[logs.length];
        int i=0;
        for(String log: letterLogs){
            result[i++] = log;
        }
        for(String log: digitLogs){
            result[i++] = log;
        }
        
        return result;
    }
}

Reorder Data In Log Files Solution Code

1