aboutsummaryrefslogtreecommitdiff
path: root/custom_sort_string.py
blob: 07de3052be6f24314f90eb4caa9b75734032c575 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 791. Custom Sort String
from collections import Counter

"""
given two strings 'order' and 's'. all the characters of 'order' are unique
and were sorted in some custom order previously. permute the characters of
's' so that they atch the order that 'order' was sorted. more specifically,
if a character 'x' occurs before character 'y' in 'order', then 'x' should
occur before 'y' in permuted string. return any permutation of 's' that
satisfies this property.
"""


class Solution(object):
    def customSortString(self, order, s):
        """
        :type order: str
        :type s: str
        :rtype: str
        """
        cnt = Counter(s)
        ans = [cnt.pop(c, 0) * c for c in order]
        for c, v in cnt.items():
            ans.append(c * v)
        return "".join(ans)


if __name__ == "__main__":
    obj = Solution()
    print(obj.customSortString("cba", "abcd"))
    print(obj.customSortString("bcafg", "abcd"))