第八周(周六)LeetCode

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LRH2018/article/details/80154292

#791

给出字符串S和T,把T中的字符按照S中字符出现的顺序排序,(S中不会重复,没有出现的可以随意位置)。

这题只需要按照S中出现次序的字符一个个从T中找出来,然后放到需要返回的字符串里面,最后再把没有出现过的字符加在最后就可以了。

代码

class Solution:
    def customSortString(self, S, T):
        r = ''
        for i in range(len(S)):
            x = T.count(S[i])
            for j in range(x):
                r += S[i]
        for x in T:
            if x not in S:
                r += x
        return r
        


猜你喜欢

转载自blog.csdn.net/LRH2018/article/details/80154292