Saturday, April 21, 2018

Leetcode 791. Custom Sort String

S and T are strings composed of lowercase letters. In S, no letter occurs more than once.
S was sorted in some custom order previously. We want to permute the characters of T so that they match the order that S was sorted. More specifically, if x occurs before y in S, then x should occur before y in the returned string.
Return any permutation of T (as a string) that satisfies this property.
Example :
Input: 
S = "cba"
T = "abcd"
Output: "cbad"
Explanation: 
"a", "b", "c" appear in S, so the order of "a", "b", "c" should be "c", "b", and "a". 
Since "d" does not appear in S, it can be at any position in T. "dcba", "cdba", "cbda" are also valid outputs.

Note:
  • S has length at most 26, and no character is repeated in S.
  • T has length at most 200.
  • S and T consist of lowercase letters only.

Analysis:
The problem is simply a merge sort. 

Code (Java):
class Solution {
    public String customSortString(String S, String T) {
        int[] dict = new int[26];
        
        for (int i = 0; i < dict.length; i++) {
            dict[i] = Integer.MAX_VALUE;
        }
        
        // Step 1: note the position of each char in S
        //
        for (int i = 0; i < S.length(); i++) {
            dict[S.charAt(i) - 'a'] = i;
        }
        
        // Step 2: merge sort
        //
        char[] Aux = new char[T.length()];
        char[] A = T.toCharArray();
        customMergeSort(dict, A, Aux, 0, A.length - 1);
        
        return String.copyValueOf(A);
    }
    
    private void customMergeSort(int[] dict, char[] A, char[] Aux, int lo, int hi) {
        
        if (lo >= hi) {
            return;
        }
        
        int mid = lo + (hi - lo) / 2;
        
        customMergeSort(dict, A, Aux, lo, mid);
        customMergeSort(dict, A, Aux, mid + 1, hi);
        
        customMerge(dict, A, Aux, lo, mid, hi);
    }
    
    private void customMerge(int[] dict, char[] A, char[] Aux, int lo, int mid, int hi) {
        for (int i = lo; i <= hi; i++) {
            Aux[i] = A[i];
        }
        
        int i = lo;
        int j = mid + 1;
        
        for (int k = lo; k <= hi; k++) {
            if (i > mid) {
                A[k] = Aux[j++];
            } else if (j > hi) {
                A[k] = Aux[i++];
            } else if (dict[Aux[i] - 'a'] < dict[Aux[j] - 'a']) {
                A[k] = Aux[i++];;
            } else if (dict[Aux[i] - 'a'] >= dict[Aux[j] - 'a']) {
                A[k] = Aux[j++];
            }
        }
    }
}

A linear time solution:
class Solution {
    public String customSortString(String S, String T) {
        // Step 1: count the freq of each char in T
        //
        int[] freq = new int[26];
        
        for (int i = 0; i < T.length(); i++) {
            char c = T.charAt(i);
            freq[c - 'a']++;
        }
        
        // step 2: scan the string S and print the number of chars in T
        //
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < S.length(); i++) {
            char c = S.charAt(i);
            
            while (freq[c - 'a'] > 0) {
                sb.append(c);
                freq[c - 'a']--;
            }
        }
        
        // step 3: scan the freq again and append anything not zero
        //
        for (int i = 0; i < freq.length; i++) {
            while (freq[i] > 0) {
                sb.append((char) (i + 'a'));
                freq[i]--;
            }
        }
        
        return sb.toString();
    }
}

No comments:

Post a Comment