[抄题]:

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字符串保持字母一样,位置可以变的更多相关文章

  1. 791. Custom Sort String - LeetCode

    Question 791. Custom Sort String Solution 题目大意:给你字符的顺序,让你排序另一个字符串. 思路: 输入参数如下: S = "cba" T ...

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

  3. 791. Custom Sort String

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...

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

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

  5. LeetCode 791. Custom Sort String

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

  6. [LeetCode] Custom Sort String 自定义排序的字符串

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...

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

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

  9. C++学习38 string字符串的增删改查

    C++ 提供的 string 类包含了若干实用的成员函数,大大方便了字符串的增加.删除.更改.查询等操作. 插入字符串 insert() 函数可以在 string 字符串中指定的位置插入另一个字符串, ...

随机推荐

  1. openstack--9--深入理解虚拟机

    登录计算节点查看进程 [root@linux-node2 ~]# ps aux | grep kvm root 824 0.0 0.0 0 0 ? S< 10:19 0:00 [kvm-irqf ...

  2. 【python】变量的赋值、深浅拷贝

    python——赋值与深浅拷贝 https://www.cnblogs.com/Eva-J/p/5534037.html 啥都不说,看这个博主的文章!

  3. Python NLP完整项目实战教程(1)

    一.前言 打算写一个系列的关于自然语言处理技术的文章<Python NLP完整项目实战>,本文算是系列文章的起始篇,为了能够有效集合实际应用场景,避免为了学习而学习,考虑结合一个具体的项目 ...

  4. 算法实践--最长递增子序列(Longest Increasing Subsquence)

    什么是最长递增子序列(Longest Increasing Subsquence) 对于一个序列{3, 2, 6, 4, 5, 1},它包含很多递增子序列{3, 6}, {2,6}, {2, 4, 5 ...

  5. (整理)MySQL_REHL6.5 安装MySQL5.5

    1 根据系统选择Mysql版本 https://dev.mysql.com/downloads/mysql/5.5.html?os=31&version=5.1 在命令行的方式下,REHL/C ...

  6. 通过spark sql 将 hdfs上文件导入到mongodb

    功能:通过spark sql 将hdfs 中文件导入到mongdo 所需jar包有:mongo-spark-connector_2.11-2.1.2.jar.mongo-java-driver-3.8 ...

  7. 未预期的符号 `$'{\r'' 附近有语法错误

    ../runcmake: 行 2: $'\r': 未找到命令 ../runcmake: 行 3: 未预期的符号 `$'{\r'' 附近有语法错误 考虑到代码是从windows下一直过来的,脚本可能在格 ...

  8. Android 梯形进度条、下载进度条;

    额,Gif有点卡: 梯形.矩形.圆角.背景色.前景色.进度条中的文字都可以改: <?xml version="1.0" encoding="utf-8"? ...

  9. 源码的excel导入导出

    获取所有数据,将数据进行有序切割,在进行遍历,将其导出. //设置header header("content-type:text/html;charset=utf-8"); // ...

  10. test case VS test scenario

    ---恢复内容开始--- 1. test case: how to test --如何测试 test scenario: what to  be tested  --测试什么 2. test scen ...