# Time: O(n) # Space: O(1) import collections class Solution(object): def customSortString(self, S, T): """ :type S: str :type T: str :rtype: str """ counter, s = collections.Counter(T), set(S) result = [c*counter[c] for c in S] result.extend([c*counter for c, counter in counter.iteritems() if c not in s]) return "".join(result)