题目链接:https://leetcode.com/problems/custom-sort-string/description/

S and T are strings composed of lowercase letters. In S, no letter occurs more than once.

S was sorted in some custom order previously. We want to permute the characters of T so that they match the order that S was sorted. More specifically, if x occurs before y in S, then x should occur before y in the returned string.

Return any permutation of T (as a string) that satisfies this property.

Example :
Input:
S = "cba"
T = "abcd"
Output: "cbad"
Explanation:
"a", "b", "c" appear in S, so the order of "a", "b", "c" should be "c", "b", and "a".
Since "d" does not appear in S, it can be at any position in T. "dcba", "cdba", "cbda" are also valid outputs.

Note:

  • S has length at most 26, and no character is repeated in S.
  • T has length at most 200.
  • S and T consist of lowercase letters only.

此题利用python自带电池的OrderedDict非常好做,loop两遍这个dict就可以了,不敢相信是medium。。。

代码如下:

class Solution(object):
def customSortString(self, S, T):
"""
:type S: str
:type T: str
:rtype: str
"""
d = collections.OrderedDict()
for c in S:
d[c] = ''
for c in T:
if c in d:
d[c] += c
else:
d[c] = c
res = ''
for k,v in d.iteritems():
res += v
return res

LeetCode 791. Custom Sort String的更多相关文章

  1. [leetcode]791. Custom Sort String自定义排序字符串

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...

  2. 791. Custom Sort String - LeetCode

    Question 791. Custom Sort String Solution 题目大意:给你字符的顺序,让你排序另一个字符串. 思路: 输入参数如下: S = "cba" T ...

  3. 【LeetCode】791. Custom Sort String 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 按顺序构造字符串 排序 日期 题目地址:https: ...

  4. 791. Custom Sort String

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...

  5. 791. Custom Sort String字符串保持字母一样,位置可以变

    [抄题]: S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S wa ...

  6. [LeetCode] Custom Sort String 自定义排序的字符串

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...

  7. 73th LeetCode Weekly Contest Custom Sort String

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...

  8. [Swift]LeetCode791. 自定义字符串排序 | Custom Sort String

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...

  9. [Javascript] Use a custom sort function on an Array in Javascript

    Sorting in Javascript with sort uses lexical sorting by default, which means it will sort in alphabe ...

随机推荐

  1. Ubuntu 将其他盘挂载到/home的子目录下

    Ubuntu 14.04 将其他盘挂载到/home的子目录下当安装完Ubuntu系统,由于当时没有注意,分配的分区空间太小.经过一段时间安装了各式各样的软件后,常常会遇到/home目录下空间不够的情况 ...

  2. 用chrome和anywhere,配合安卓机搭建最简单的移动端页面测试。

    很多时候,我们前端在写移动端页面的时候,虽然目前chrome有调试模式,可以模拟手机的部分效果,但仍有部分效果需要直接在手机上进行页面的调试,今天就在这里推荐一个适合windows+安卓的无需连接局域 ...

  3. linux文本处理命令

    linux文本处理命令 1.wc命令 基本介绍 文件的行统计.字符统计.字节统计 基本语法 wc  [OPTION]...  [FILE]... wc  [OPTION]...  --files0-f ...

  4. javap反汇编命令

    https://blog.csdn.net/qq_36330643/article/details/73841313 有关反汇编的具体

  5. java标志性接口

    标识接口是没有任何方法和属性的接口.它仅仅表明它的类属于一个特定的类型,供其他代码来测试允许做一些事情.使用标记接口的唯一目的是使得可以用instanceof进行类型查询,例如:if(obj inst ...

  6. 2018.5.3 maven

     1  maven基本概念  1.1maven是什么 1)软件项目管理和理解工具      2)项目对象模型(Project Object Model,POM)      3)项目的构建.报告和文档的 ...

  7. 关于crontab

    crontab是一个linux系统自带的定时执行任务的功能,有两种方法可以实现 1: 使用命令 crontab -e 然后直接编辑定时脚本,实际是编辑/var/spool/cron 目录下,一个和用户 ...

  8. Jenkins可持续集成项目搭建——配置Jenkins基本设置 & 运行脚本报错点

    一.系统管理->全局工具配置:配置JDK.GIT: 二.项目配置-部署源码管理的远程地址 三.项目配置-构建触发器(以Windows平台举例) 构建项目时,会自动分配一个运行空间(D:\Prog ...

  9. Java CAS同步机制 实践应用

    利用CAS实现原子操作类AtomicInteger (这是自定义的AtomicInteger:java有封装好的原子操作AtomicInteger类): class AtomicInteger { p ...

  10. Go 字符串连接+=与strings.Join性能对比

    Go字符串连接 对于字符串的连接大致有两种方式: 1.通过+号连接 func StrPlus1(a []string) string { var s, sep string for i := 0; i ...