LeetCode 791 Custom Sort String (Python)

Posted by 小明MaxMing on November 1, 2024

题目

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.

解题思路

方法一,用字典给每个字母一个数作为大小比较,没出现在order里的默认为-1,自定义排序比较函数

方法二,用counter求出每个字母出现的次数,遍历order,如果遇到出现的字母,就产生对此个数的字母,把所有没出现在order里的放到最后

代码

class Solution:
    def customSortString(self, order: str, str: str) -> str:
        ct = Counter(str)
        res = []
        for c in order:
            res.append(c * ct[c])
            ct[c] = 0
        for c, v in ct.items():
            res.append(c * v)
        return ''.join(res)
class Solution:
    def customSortString(self, order: str, str: str) -> str:
        d = {c: i for i, c in enumerate(order)}
        return ''.join(sorted(str, key=lambda x: d.get(x, -1)))

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅