当前位置:网站首页>leetcode-791. Custom string sorting
leetcode-791. Custom string sorting
2022-04-23 12:42:00 【Programmed ape no hair loss 2】
Given two strings order and s .order All the words are only Of , And it used to be sorted in some custom order .
Yes s Replace the characters of , Align it with the sorted order Match . More specifically , If in order The characters in x The character... Appears y Before , Then in the arranged string , x It should also appear in y Before .
return Satisfying this property s Arbitrary arrangement of .
Example 1:
Input : order = “cba”, s = “abcd”
Output : “cbad”
explain :
“a”、“b”、“c” It appears in order , therefore “a”、“b”、“c” The order of should be “c”、“b”、“a”.
because “d” Not in order , So it can be anywhere in the returned string .“dcba”、“cdba”、“cbda” It is also a valid output .
Example 2:
Input : order = “cbafg”, s = “abcd”
Output : “cbad”
Tips :
1 <= order.length <= 26
1 <= s.length <= 200
order and s It's made up of lowercase letters
order All characters in the are Different
java Code :
class Solution {
public String customSortString(String order, String s) {
int[] count = new int[26];
for (int i = 0; i < s.length(); i++) {
count[s.charAt(i) - 'a']++;
}
StringBuilder sbd = new StringBuilder();
for (char c : order.toCharArray()) {
for (int i = 0; i < count[c - 'a']; i++) {
sbd.append(c);
}
count[c - 'a'] = 0;
}
for (char c = 'a'; c <= 'z'; c++) {
for (int i = 0; i < count[c - 'a']; i++) {
sbd.append(c);
}
}
return sbd.toString();
}
}
版权声明
本文为[Programmed ape no hair loss 2]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231239410202.html
边栏推荐
- Idea的src子文件下无法创建servlet
- C set Logo Icon and shortcut icon
- leetcode:437. 路径总和 III【dfs 选还是不选?】
- Kubernetes 入门教程
- C#,二维贝塞尔拟合曲线(Bézier Curve)参数点的计算代码
- Can I take the CPDA data analyst certificate for 0 foundation
- 风尚云网学习-h5的input:type属性的image属性
- [daily question] chessboard question
- STM32 control stepper motor (ULN2003 + 28byj)
- Uni app native app local packaging integrated Aurora push (jg-jpush) detailed tutorial
猜你喜欢
STM32 project transplantation: transplantation between chip projects of different models: Ze to C8
【csnote】ER图
bert-base-chinese下载(智取)
Everything can be expected in the future | one 2022 campus recruitment officially opened
Idea setting copyright information
Qt进程间通信
STM32控制步进电机(ULN2003+28byj)
XinChaCha Trust SSL Organization Validated
Deploying MySQL in cloud native kubesphere
Dialogue with Bruce, author of PostgreSQL: "changing careers" is to better move forward
随机推荐
Ad20 supplementary note 3 - shortcut key + continuous update
解锁OpenHarmony技术日!年度盛会,即将揭幕!
BaseRecyclerViewAdapterHelper 实现下拉刷新和上拉加载
SQLserver怎么插入或更新当天的星期数,bit而不是文本
STM32工程移植:不同型号芯片工程之间的移植:ZE到C8
BUUCTF WEB [GXYCTF2019]禁止套娃
leetcode:437. Path sum III [DFS selected or not selected?]
Dialogue with Bruce, author of PostgreSQL: "changing careers" is to better move forward
栈和队列a
[wechat applet] Z-index is invalid
STM32 control stepper motor (ULN2003 + 28byj)
C set Logo Icon and shortcut icon
BUUCTF WEB [BUUCTF 2018]Online Tool
Outsourcing for five years, abandoned
Qt一个进程运行另一个进程
QT redraw events and cuts
MySQL函数-递归函数
What are the forms of attack and tampering on the home page of the website
Web17——EL与JSTL的使用
Aviation core technology sharing | overview of safety characteristics of acm32 MCU