LeetCode 791. Custom Sort String
题目链接:https://leetcode.com/problems/custom-sort-string/description/
SandTare strings composed of lowercase letters. InS, no letter occurs more than once.
Swas sorted in some custom order previously. We want to permute the characters ofTso that they match the order thatSwas sorted. More specifically, ifxoccurs beforeyinS, thenxshould occur beforeyin 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:
Shas length at most26, and no character is repeated inS.Thas length at most200.SandTconsist 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 ...
随机推荐
- Spring框架:@ResponseBody 中文乱码----------我的主题站内单点登录
问题背景 本文并不是介绍@ResponseBody注解,也不是中文乱码问题的大汇总笔记,这些网上都有很多内容了.这边仅对几年前,一个卡壳了挺久时间的问题的解决过程做一个记录,以警惕自己,达到自醒得目的 ...
- vue 配合vue-resource调用接口,获取数据
1.先用node+express+mysql简单配置一下后台 const express = require('express');const mysql = require('mysql');con ...
- android 版本更新适配8.0,解决8.0手机无法更新自动安装apk
随着android 7.0的普及android 8.0的也逐渐流行起来,那么google对权限方面又有了新的修改.而且我发现在android8.0中除了一些bug,比如说:在小米6(Android 8 ...
- 根据某字段将其他字段进行拼接的两种方法(SYS_CONNECT_BY_PATH及wm_concat)
秘书姐姐说想知道她发起的所有流程,现在都到谁审批了.由于一条流程当前审批人可能有多个,故需根据单据编号(djbh)将审批人拼接到一个字段中. 说明: wfn审批历史记录表,djbh 单据编号,pk_c ...
- 21. Wireless tools (无线工具 5个)
AircrackKismetNetStumblerinSSIDerKisMAC
- 关于定时器setTimeout()方法的实践--巧解bug
_使用开发环境:UAP:_ _框架:JQuery.MX:_ 最近的开发的页面中,有一处需要在提交的 datagrid里启用行编辑,就会发生奇怪的bug,编辑过程中如图所示不移开焦点直接点保存,那么已输 ...
- 使用Visual Studio 2017 C++17模块(module)特性
环境: win7_x64旗舰版.VS2017企业版 一.安装VS2017 1.1 安装VS2017时,必须要勾选“使用C++的modules开发”选项 1.2 安装成功后,会在VS安装目录Micros ...
- 使用User Primary Email作为GUID的问题
最近发现有人使用CRM的user primary email作为GUID, 并且做了plugin来控制user primary email. 这样做法是非常有问题而且会影响同名的再次注册的用户. 假如 ...
- 我发起了一个 用 C# 写 的 浏览器 开源项目 HtmlCore
我之前还发起过一个 项目, 名字也叫 HtmlCore, 见 <我发起了一个 .Net 开源 跨平台 GUI (界面开发框架)项目 HtmlCore> https://www.cnblo ...
- Devexpress gridview cell增加控件
1.根据上图次序,先添加三类控件类型: 2.根据上图次序设置columnEdit 3.点开ColumnEdit , 设置 check 和 uncheck的 value