题目链接: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. There is no getter for property named 'user' in 'class com.jyr.wh.domain.User' 异常

    今天在使用mybatis时,出现了一个问题:There is no getter for property named 'user' in 'class com.jyr.wh.domain.User, ...

  2. flask 图文混排的简单操作

    1.引入js 包<script type="text/javascript" src="../../static/news/js/jquery-1.12.4.min ...

  3. 常用的phpdoc标签

    标签 说明 @access public|private|protected 描述了访问级别.当使用反射技术时,这个标签不是很有用,这是因为API能够自动获取这一特性.在PHPDoc中,用它可略去私有 ...

  4. 浅谈session和cookie

    含义: session(会话):指用户登录网站后的一系列动作,比如浏览商品添加到购物车并购买 cookie:用户身份的一种标识 区别: 1.Cookie通过在客户端记录信息确定用户身份,Session ...

  5. swiper插件使用遇到的一点小问题

    最近做移动端开发 给出的静态页使用了Swiper,用的是4.0.3版本,应该是比较新的. 静态页这种东西,一般就是给你个雏形,设计部虽然使用了这个插件,但毕竟这个活儿毕竟还是得开发人员来干,所以,静态 ...

  6. Windows下部署Apache RocketMQ

    一:环境准备: Windows.JDK1.8+.Maven.Git 二:RocketMQ准备:  1.http://rocketmq.apache.org/release_notes/release- ...

  7. sublime An unhandled OS error was encountered nodejspath_error

    sublime An unhandled OS error was encountered  nodejspath_error 点击ok,修改node_path typescript 插件下载 ctr ...

  8. Ajaxpro使用的方法

    1.下载Ajaxpro.2.dll 程序中引用 2.web.config配置 <?xml version="1.0" encoding="utf-8"?& ...

  9. PTA8

    这个作业属于哪个课程 C语言程序设计2 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/software-engineering-class2-2018/ ...

  10. APIPA

    自动专用IP地址(Automatic Private IP Address,APIPA)是当客户端无法从DHCP服务器中获得IP地址时自动配置的地址.IPv4地址前缀169.254/16已经被IANA ...