[LeetCode] Custom Sort String 自定义排序的字符串
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 most26
, and no character is repeated inS
.T
has length at most200
.S
andT
consist of lowercase letters only.
这道题给了我们两个字符串S和T,让我们将T按照S的顺序进行排序,就是说在S中如果字母x在字母y之前,那么排序后的T中字母x也要在y之前,其他S中未出现的字母位置无所谓。那么我们其实关心的是S中的字母,只要按S中的字母顺序遍历,对于遍历到的每个字母,如果T中有的话,就先排出来,这样等S中的字母遍历完了,再将T中剩下的字母加到后面即可。所以我们先用HashMap统计T中每个字母出现的次数,然后遍历S中的字母,只要T中有,就将该字母重复其出现次数个,加入结果res中,然后将该字母出现次数重置为0。之后再遍历一遍HashMap,将T中其他字母加入结果res中即可,参见代码如下:
解法一:
class Solution {
public:
string customSortString(string S, string T) {
string res = "";
unordered_map<char, int> m;
for (char c : T) ++m[c];
for (char c : S) {
res += string(m[c], c);
m[c] = ;
}
for (auto a : m) {
res += string(a.second, a.first);
}
return res;
}
};
下面这种解法的思路和上面的一样,只不过这里没有使用HashMap,而是使用了一个长度为26的数组,因为题目中说了S和T中都是小写的字母,其他部分没有啥太大的区别,参见代码如下:
解法二:
class Solution {
public:
string customSortString(string S, string T) {
string res = "";
vector<int> cnt(, );
for (char c : T) ++cnt[c - 'a'];
for (char c : S) {
while (cnt[c - 'a']-- > ) res.push_back(c);
}
for (char c : T) {
while (cnt[c - 'a']-- > ) res.push_back(c);
}
return res;
}
};
下面这种方法可以说是简洁的让人发指啊,就两行搞定碉堡了。我们自定义了sort的排序的排序方式,对于字符串T中的任意两个字母a和b,按照其在S中的顺序排序,在S中排前面的在T中也排前面,完全符合题意,所以就能很好的work,参见代码如下:
解法三:
class Solution {
public:
string customSortString(string S, string T) {
sort(T.begin(), T.end(), [&](char a, char b) {return S.find(a) < S.find(b);});
return T;
}
};
下面这种解法没有用到STL中内置的find函数,而是用了HashMap来建立S中每个字母和其出现位置之间的映射,这样在自定义排序方法的时候,就可以直接从HashMap中取位置了,参见代码如下:
解法四:
class Solution {
public:
string customSortString(string S, string T) {
unordered_map<char, int> m;
for (int i = ; i < S.size(); ++i) {
m[S[i]] = i + ;
}
sort(T.begin(), T.end(), [&](char a, char b) {return m[a] < m[b];});
return T;
}
};
类似题目:
https://leetcode.com/problems/custom-sort-string/solution/
https://leetcode.com/problems/custom-sort-string/discuss/116556/Two-Lines-C++
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 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 ...
- Arrays.sort(a) 自定义排序
Arrays.sort(a) 自定义排序,(需实现接口:Comparable) package com.hd; import java.util.Arrays; class Person imple ...
- [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 ...
- 【LeetCode】791. Custom Sort String 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 按顺序构造字符串 排序 日期 题目地址:https: ...
- 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 ...
- LeetCode 791. Custom Sort String
题目链接:https://leetcode.com/problems/custom-sort-string/description/ S and T are strings composed of l ...
- 791. Custom Sort String字符串保持字母一样,位置可以变
[抄题]: S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S wa ...
- [LeetCode] Special Binary String 特殊的二进制字符串
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
随机推荐
- postgreSql 常用操作总结
0. 启动pgsl数据库 pg_ctl -D /xx/pgdata start 1. 查看pgsl版本 pg_ctl --version 1. 命令行登录数据库 psql -U username -d ...
- Java String相关
一.String类的常用方法 1. int indexOf(String s) 字符串查找 2. int lastIndexOf(String str) 3. char charAt(int inde ...
- Erdos
Erdős Pál(1913年3月26日-1996年9月20日),匈牙利籍犹太人,发表论文达1475篇(包括和人合写的),为现时发表论文第二多的数学家(第一是Euler):曾和509人合写论文. Er ...
- [物理学与PDEs]第2章第4节 激波 4.2 熵条件
1. R.H. 条件仅仅给出了越过激波时的能量守恒定律, 即热力学第一定律; 但客观的流体运动过程还需满足热力学第二定律, 即越过激波是个熵增过程: $$\bex S_1>S_0\quad(0 ...
- 使用系统用户登录Oracle
如果数据库安装不在本机上,@后面加的是服务名或IP地址 如果是sys用户的话,它具有管理员的权限,要使用sysdba或sysoper权限来登录oracle工具.
- 移动端bug集合
移动端软键盘遮挡输入框&IOS移动端点击输入框字体放大&IOS点击闪烁 移动端软键盘遮挡输入框 ——> 转载自: https://www.jb51.net/article/1 ...
- javascipt继承机制(from阮一峰)
Javascript继承机制的设计思想 我一直很难理解Javascript语言的继承机制. 它没有"子类"和"父类"的概念,也没有"类" ...
- C++反汇编调试
1.使用 OllyDBG打开的dll文件,最好找破解pro版本.不然没有编辑权限 ,目前OllyDBG并不支持eclipse IDE 64位编辑的 .class文件类型. 另外使用反编译的时候物理内 ...
- bzoj 3238
后缀数组+单调栈的应用 首先我们研究一下这个表达式,可以发现前半部分与串的情况并没有关系,而只是跟串的长度有关,所以我们先把前半部分算出来: 于是我们只需计算出即可 那么可以发现,对于排名分别为i,j ...
- HttpClient当HTTP连接的时候出现大量CLOSE_WAIT连接(转)
ESTABLISHED 表示正在进行网络连接的数量 TIME_WAIT 表示表示等待系统主动关闭网络连接的数量 CLOSE_WAIT 表示被动等待程序关闭的网络连接数量 上篇文章给出了解决TIME_W ...