Skip to content
LC-0791 Medium LeetCode

791. Custom Sort String

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 72% Topics: Hash Table, String, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# 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)

Solution from kamyu104/LeetCode-Solutions · MIT