Custom sort string
December 24, 2022
array-and-hashmapProblem URL: Custom sort string
We will use a hashmap to store the order of characters in order
. We will iterate over s
and store the characters in a list. We will sort the list using the order of characters in order
. We will return the res.
class Solution:
def customSortString(self, order: str, s: str) -> str:
res = ''
count = collections.Counter(s)
for ch in order:
res += ch * count[ch]
del count[ch]
res += ''.join(cnt*count[cnt] for cnt in count)
return res
Time complexity: O(n)
Space complexity: O(n)