Saturday, January 5, 2019

791. Custom Sort String




Version #1 Bucket Sort

Time O(S.length() + T.length())

70.74 %
class Solution {
    public String customSortString(String S, String T) {
        StringBuilder sb = new StringBuilder();
        int[] count = new int[26];
        for (int i = 0; i < T.length(); i++) {
            count[T.charAt(i) - 'a']++;
        }
        for (int i = 0; i < S.length(); i++) {
            char c = S.charAt(i);
            while (count[c - 'a']-- > 0) {
                sb.append(c);
            }
        }
        for (int i = 0; i < 26; i++) {
            while (count[i]-- > 0) {
                sb.append((char)(i + 'a'));
            }
        }
        return sb.toString();
    }
}

No comments:

Post a Comment