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.

这道题给了我们两个字符串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++

https://leetcode.com/problems/custom-sort-string/discuss/116615/Java-5-ms-10-line-counting-solution-with-comment

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 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. Arrays.sort(a) 自定义排序

     Arrays.sort(a) 自定义排序,(需实现接口:Comparable) package com.hd; import java.util.Arrays; class Person imple ...

  4. [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 ...

  5. 【LeetCode】791. Custom Sort String 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 按顺序构造字符串 排序 日期 题目地址:https: ...

  6. 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 ...

  7. LeetCode 791. Custom Sort String

    题目链接:https://leetcode.com/problems/custom-sort-string/description/ S and T are strings composed of l ...

  8. 791. Custom Sort String字符串保持字母一样,位置可以变

    [抄题]: S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S wa ...

  9. [LeetCode] Special Binary String 特殊的二进制字符串

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

随机推荐

  1. 关于设计项目UI界面的软件工具

    关于画UI界面的软件,我在网上找了几个,今天式用这几款软件还可以 1.墨刀:国产的,这个专门画APP界面的,用起来比较简单,有免费版的,要注册才能用,提供云存储,收费版的云存储空间会多一些.网站: h ...

  2. Java多线程:向线程传递参数的三种方法

    在传统的同步开发模式下,当我们调用一个函数时,通过这个函数的参数将数据传入,并通过这个函数的返回值来返回最终的计算结果.但在多线程的异步开发模式下,数据的传递和返回和同步开发模式有很大的区别.由于线程 ...

  3. PYTHON语言之常用内置函数

    一 写在开头本文列举了一些常用的python内置函数.完整详细的python内置函数列表请参见python文档的Built-in Functions章节. 二 python常用内置函数请注意,有关内置 ...

  4. oldboy s21day09

    #!/usr/bin/env python# -*- coding:utf-8 -*- # 1.将函数部分知识点,整理到自己笔记中.(搞明白课上讲的案例.) # 2.写函数,检查获取传入列表或元组对象 ...

  5. [Android] 免费天气预报接口

    [Android] 免费天气预报接口 这是 国家气象局提供的天气预报接口 [免费] 当然,网上有很多的收费API或者每天定次数的接口 使用 国家气象局 的步骤如下: 1.首先获取城市ID号 北京:10 ...

  6. ArcGIS Server 10.0 安装及使用完整攻略

    引言 ArcGIS Server 10.0在使用和安装的过程中,需要进行比较全面的学习,才能正确使用.缺乏正确的指引,用户很容易在安装及使用中遇到问题.所以笔者在此总结Server 10.0的安装及使 ...

  7. SrpingBoot部署到云服务器

    预先准备事项 1.本地主机:安装maven 2.云端主机:安装和配置jdk 一.maven打包 方式一:maven手动版 切换至项目下,cmd:mvn package 查看target目录: 方式二: ...

  8. css Modules 使用

    我目前常用的也就是引用类名,以及在需要修改某个ui组件原有的样式比较麻烦时,会使用 :global className{ color: red;}这样来修改... 更多请参考阮老师博客: http:/ ...

  9. springMVC源码笔记

    springMVC 设计总览 下图来源:https://www.cnblogs.com/fangjian0423/p/springMVC-directory-summary.html 下图来源:htt ...

  10. Django部署方法

    Windows方案: Apache2.4 + Django2.0 网上的方法乱七八糟: 那么接下来:最好的方法,不行吃屎. 当前环境是Django2.0+ python35(64bit) 部署原因: ...