给定两个字符串 order 和 s 。order 的所有单词都是 唯一 的,并且以前按照一些自定义的顺序排序。
对 s 的字符进行置换,使其与排序的 order 相匹配。更具体地说,如果在 order 中的字符 x 出现字符 y 之前,那么在排列后的字符串中, x 也应该出现在 y 之前。
返回 满足这个性质的 s 的任意排列 。
class Solution:
def customSortString(self, order: str, s: str) -> str:
s = list(s)
res = ""
for c in order:
for item in s:
if c == item:
res = res+c
index = s.index(item)
s[index] = "#"
for item in s:
if item != "#":
res += item
return res