• 791. 自定义字符串排序


    字符串S和 T 只包含小写字符。在S中,所有字符只会出现一次。

    S 已经根据某种规则进行了排序。我们要根据S中的字符顺序对T进行排序。更具体地说,如果S中x在y之前出现,那么返回的字符串中x也应出现在y之前。

    返回任意一种符合条件的字符串T。

    示例:
    输入:
    S = "cba"
    T = "abcd"
    输出: "cbad"
    解释:
    S中出现了字符 "a", "b", "c", 所以 "a", "b", "c" 的顺序应该是 "c", "b", "a".
    由于 "d" 没有在S中出现, 它可以放在T的任意位置. "dcba", "cdba", "cbda" 都是合法的输出。
    注意:

    S的最大长度为26,其中没有重复的字符。
    T的最大长度为200。
    S和T只包含小写字符。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/custom-sort-string
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    class Solution:
        def customSortString(self, a: str, b: str) -> str:
            difference= list(set(b).difference(set(a)))
            intersection = list(set(a) & set(b))
            cntb=collections.Counter(b)
            cntDiff=collections.Counter(difference)
            cntInter=collections.Counter(intersection)
            res=[]
            for i in a:
                if cntInter[i]==1:
                    res+=[i]*cntb[i]
            for i in difference:
                if cntDiff[i]==1:
                    res+=[i]*cntb[i]
            return ''.join(res) 
    class Solution:
        def customSortString(self, a: str, b: str) -> str:
            d = {c: i for i, c in enumerate(a)}
            return ''.join(sorted(b, key=lambda c: d.get(c, 1)))
    class Solution:
        def customSortString(self, a: str, b: str) -> str:
            return ''.join(sorted(list(b), key=lambda x:a.find(x)))
  • 相关阅读:
    登乐游原
    遇到Tomcat端口占用怎么办
    tensorflow cnn+rnn基本结构
    linux bash 入门
    python 装饰器
    php 后端开发学习
    图像增强方法
    git 使用
    斯坦福机器学习课程笔记
    django学习笔记
  • 原文地址:https://www.cnblogs.com/xxxsans/p/13833624.html
Copyright © 2020-2023  润新知