题目链接: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. ssm配置文件叙述

    spring+springmvc+mybatis框架中用到了三个XML配置文件:web.xml,spring-mvc.xml,spring-mybatis.xml.第一个不用说,每个web项目都会有的 ...

  2. 查询MySQL数据库中表结构的几种方法

    什么是表结构?表结构就是定义数据表文件名,确定数据表包含哪些字段,各字段的字段名.字段类型.及宽度,并将这些数据输入到计算机当中. 查询方法:以表‘employees’为例子 1.describe(d ...

  3. 解决 Unknown action buyram in contract eosio 错误

  4. TCP/UDP 网络工具

    1. 统计连接状态 netstat -n | awk '/^tcp/ {++State[$NF]} END {for(s in State) print s, State[s]}' CLOSE_WAI ...

  5. bean 属性排列顺序

    丑是丑了点,但是有效啊.@JsonPropertyOrder({ "IP", "accuracy", "continent", " ...

  6. kettle之linux使用kettle

    Kettle可以在Window.Linux.Unix上运行,数据抽取高效稳定,使用之前需要准备环境. 准备java环境,这里就不赘述了,建议jdk7以上版本. 上传kettle压缩包,并解压,我解压的 ...

  7. Gym101889J. Jumping frog(合数分解+环形dp预处理)

    比赛链接:传送门 题目大意: 一只青蛙在长度为N的字符串上跳跃,“R”可以跳上去,“P”不可以跳上去. 字符串是环形的,N-1和0相连. 青蛙的跳跃距离K的取值范围是[1, N-1],选定K之后不可改 ...

  8. mysql trigger 备忘

    最近用mysql有这么一个需求 item表:id,item,url,websiteid website表:id,domain item表示从不同网站获取的信息 website表示获得信息的网站,其中的 ...

  9. Development descriptor

    部署描述符指的是配置文件对于一个假象部署到一些容器/发动机. 在Java平台,企业版部署描述符描述组件.模块或应用程序(例如web应用程序或者企业应用程序)应该被部署.它指导部署工具部署具有特定容器选 ...

  10. c# 枚举安卓系统中所有目录及文件名

    using Android.App; using Android.Widget; using Android.OS; using System.Runtime.InteropServices; nam ...