Leetcode 807 Solution

This article provides solution to leetcode question 807 (custom-sort-string)

https://leetcode.com/problems/custom-sort-string

Solution

class Solution:
    def customSortString(self, S: str, T: str) -> str:
        letters = [0] * 26
        for t in T:
            letters[ord(t) - ord('a')] += 1

        ans = ""
        for s in S:
            ans += s * letters[ord(s) - ord('a')]
            letters[ord(s) - ord('a')] = 0

        for i, letter in enumerate(letters):
            ans += chr(ord('a') + i) * letter

        return ans