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 字符串中指定的位置插入另一个字符串, ...
随机推荐
- Unity在WPF中的应用
1. 本文的实现类继承于IRepository using System; using System.Linq; using System.Linq.Expressions; using Zhang. ...
- RDO快速部署OpenStack
RDO快速部署OpenStack 1.RDO是什么 RDO是红帽Red Hat Enterprise Linux OpenStack Platform的社区版,类似RHEL和Fedora,RHEV和o ...
- 删除iis日志(deliislogs.vbs)
'path 目录 'ext 文件扩展名'expiredDays 保留多少天以内的文件Sub LogCleaner(path,ext,expiredDays) On Error Resume Next ...
- Optaplanner - 从探究示例中的hello world,初步认识规划引擎的运行步骤。
上一篇我们成功以把Opotaplanner规划引擎下载回来,并把它的示例运行起来,简单解析了一下它的Cloud balance示例.这一篇我们这些示例的源代码导入到Eclipse中,看看它在后台是怎么 ...
- msvcp140.dll丢失解决方案
[首先给出99%情况下都能解决这个问题的最简单办法]: 下载并安装微软VC++2015版运行库 (英文全称:Microsoft Visual C++ 2015 Redistributable Pa ...
- Java备份文件
文件名后面补时间: public static void initFile(String sPath) { SimpleDateFormat df = new SimpleDateFormat(&qu ...
- Angularjs启动入口, splash画面,与加快启动的技巧
Angularjs启动入口, splash画面,与加快启动的技巧 Angularjs启动入口 * 自动响应DOMContentLoaded event * 从ngApp指定的入口启动: 在angula ...
- Python有趣时刻,这些代码让你大呼"卧槽,怎么会这样"
分享一个实用问题,用python读取Excel并保存字典,如何做? 下面是该同学问题截图和代码 image.png 代码截图是下面这样的 image.png 不知道大家第一眼看了这个代码,什么感受?我 ...
- Zookeeper的一致性协议:Zab
Zookeeper使用了一种称为Zab(Zookeeper Atomic Broadcast)的协议作为其一致性复制的核心,据其作者说这是一种新发算法,其特点是充分考虑了Yahoo的具体情况: ...
- 析构方法(__del__)
析构方法,当对象在内存中被释放时(也就是实例执行完了,实例的内存就会自动释放,这时候就会触发),自动触发执行. 当程序结束时,python只会回收自己的内存空间,即用户态内存,而操作系统的资源则没有被 ...