791. Custom Sort String

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 791

class Solution:
    def customSortString(self, order: str, s: str) -> str:
        cnt = Counter(s)
        res = []
        for c in order:
            res.append(c * cnt[c])
            cnt[c] = 0
        for c, v in cnt.items():
            res.append(c * v)
        return ''.join(res)