LeetCode 791. Custom Sort String
题目链接:https://leetcode.com/problems/custom-sort-string/description/
S
andT
are strings composed of lowercase letters. InS
, no letter occurs more than once.
S
was sorted in some custom order previously. We want to permute the characters ofT
so that they match the order thatS
was sorted. More specifically, ifx
occurs beforey
inS
, thenx
should occur beforey
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 most26
, and no character is repeated inS
.T
has length at most200
.S
andT
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的更多相关文章
- [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 ...
- 791. Custom Sort String - LeetCode
Question 791. Custom Sort String Solution 题目大意:给你字符的顺序,让你排序另一个字符串. 思路: 输入参数如下: S = "cba" T ...
- 【LeetCode】791. Custom Sort String 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 按顺序构造字符串 排序 日期 题目地址:https: ...
- 791. Custom Sort String
S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...
- 791. Custom Sort String字符串保持字母一样,位置可以变
[抄题]: S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S wa ...
- [LeetCode] Custom Sort String 自定义排序的字符串
S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...
- 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 ...
- [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 ...
- [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 ...
随机推荐
- 禁止用su切换到root
http://jie388.blog.51cto.com/1433454/920492
- Could not find a version that satisfies the requirement PIL
Python Imageing Library 简称 PIL Python常用的图像处理库之一 Pillow是PIL一个fork. C:\Users\dangzhengtao>pip insta ...
- Codeforces1113F. Sasha and Interesting Fact from Graph Theory(组合数学 计数 广义Cayley定理)
题目链接:传送门 思路: 计数.树的结构和边权的计数可以分开讨论. ①假设从a到b的路径上有e条边,那么路径上就有e-1个点.构造这条路径上的点有$A_{n-2}^{e-1}$种方案: ②这条路径的权 ...
- zoj 1649 bfs
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...
- laravel 还原项目到正常状态
首先回滚数据库迁移: $ php artisan migrate:rollback 还原修改文件到原始状态: $ git checkout . 查看文件修改状态: $ git status 可以看出剩 ...
- vue day5 分页控件
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 关于mysql中存储json数据的读取问题
在mysql中存储json数据,字段类型用text,java实体中用String接受. 返回前端时(我这里返回前端的是一个map),为了保证读取出的数据排序错乱问题,定义Map时要用LinkedHas ...
- redis key命令
key命令主要用于管理redis中的key del key //删除key, 不存在的key会忽略 dump key //序列化key,不存在的key返回nil exists key //判断key是 ...
- MySQL 表字段操作
MySQL 表字段操作 一.增加表字段 1)mysql> alter table 二.删除表字段 三.修改表字段
- maven无法下载oracle驱动包
由于版权问题,在中央仓库是没有oracle的jdbc驱动的,可以手动下载到本地或者在oracle安装目录的jdbc目录中找到对应的jar,然后通过以下命令安装到本地仓库 mvn install:ins ...