-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_sort_string.py
33 lines (27 loc) · 1.15 KB
/
custom_sort_string.py
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
32
33
'''
You are 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 match the order that order was sorted. More specifically, if a character x occurs before a character y in order, then x should occur before y in the permuted string.
Return any permutation of s that satisfies this property.
'''
from collections import defaultdict
class Solution:
def customSortString(self, S, T) :
ordering = defaultdict(lambda: -1, ((v, k) for (k, v) in enumerate(S)))
return ''.join(sorted(T, key=ordering.__getitem__))
--------------------------------------------------------------------------------------
from collections import Counter
class Solution:
def customSortString(self, s: str, t: str) -> str:
s = list(s)
h = Counter(t)
res = ''
for i in range(len(s)):
c = s[i]
if c in h:
for j in range(h[c]):
res += c
del h[c]
for key, value in h.items():
for i in range(value):
res += key
return res