给定两个字符串 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
该博客介绍了一个Python解决方案,用于将字符串`s`重新排列以匹配给定的排序字符串`order`。通过遍历`order`并在`s`中找到对应字符并移除,最后将剩余字符添加到结果中,实现了这一目标。
641

被折叠的 条评论
为什么被折叠?



