791. 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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
大体思路是对的:先差后s,但是还可以优化一下:先s后差。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
粘贴差字母的时候,从26个字母a-z逐个入手即可。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
粘贴差字母的时候,从26个字母a-z逐个入手即可。
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public String customSortString(String S, String T) {
//corner case
if (S == null || T == null) return "";
//initialization: int[26], count char
int[] count = new int[26];
for (char t : T.toCharArray()) {
count[t - 'a']++;
}
StringBuilder sb = new StringBuilder();
//append s
for (char s : S.toCharArray()) {
while (count[s - 'a']-- > 0)
sb.append(s);
}
//append t - s from a to z
for (char c = 'a'; c <= 'z'; c++) {
while (count[c - 'a']-- > 0)
sb.append(c);
}
return sb.toString();
}
}
791. Custom Sort String字符串保持字母一样,位置可以变的更多相关文章
- 791. Custom Sort String - LeetCode
Question 791. Custom Sort String Solution 题目大意:给你字符的顺序,让你排序另一个字符串. 思路: 输入参数如下: S = "cba" T ...
- [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
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: ...
- LeetCode 791. Custom Sort String
题目链接:https://leetcode.com/problems/custom-sort-string/description/ S and T are strings composed of l ...
- [LeetCode] 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 ...
- 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 ...
- C++学习38 string字符串的增删改查
C++ 提供的 string 类包含了若干实用的成员函数,大大方便了字符串的增加.删除.更改.查询等操作. 插入字符串 insert() 函数可以在 string 字符串中指定的位置插入另一个字符串, ...
随机推荐
- hsdfz -- 6.18 -- day3
第三次被hn菜和hn话支配…… 相比起前两天好一点,但是由于前面时间安排的太散(睡着了……)导致c题DP差一点肝出来(所以最后没有…… 恩就算肝出来DP也只有30分,这次好歹是有DP思路了,继续康复吧 ...
- Vue 表单校验 vee-validate
gitHub 地址:https://github.com/baianat/vee-validate 官网API 地址:https://baianat.github.io/vee-validate/ap ...
- 前段开发神奇webstorm安装注册和汉化
软件下载地址: http://www.jetbrains.com/webstorm/ 安装完后退出. 重新打开,进行激活 这里我们选择“license server”然后输入:http://idea. ...
- mysql识别中文
在配置的INI中加上这些 [mysql]default-character-set=utf8no-auto-rehash# Remove the next comment character if y ...
- Unity Shader Graph(一)初次尝试
软件环境 Unity Version: 2018.1.2f1 边缘发光材质效果 创建工程 打开Unity并创建一个新工程 安装依赖项 Window -> Package Manager打开包管理 ...
- .NET自动化测试工具:Selenium Grid
在生产环境,QA会同时跑几十个上百个的test case.如果用单机串行的话,是一件非常耗时的事情,估计比手点快不了多少.使用并行方案的话,有两种方法,一个是自己写并行框架,一个是用现成的Seleni ...
- (转)SQLServer查询数据库各种历史记录
原文地址https://www.cnblogs.com/seusoftware/p/4826958.html 在SQL Server数据库中,从登陆开始,然后做了什么操作,以及数据库里发生了什么,大多 ...
- Java date 日期计算
import org.junit.Test; import java.util.Calendar; import java.util.Date; /** * @author cosmo * @Titl ...
- 学习笔记:AngularJs
站点: http://www.angularjs.cn/ angularjs中文社区 http://www.jb51.net/article/60733.htm AngularJS内置指令 基本页 ...
- java编程思想(2)--一切都是对象
1创建对象 String s ;创建引用,并未初始化,即引用未关联任何东西 String s2="asda"; 初始化 System.out.println(s2); System ...