当前位置:网站首页>leetcode-791. 自定义字符串排序
leetcode-791. 自定义字符串排序
2022-04-23 12:39:00 【程序猿不脱发2】
给定两个字符串 order 和 s 。order 的所有单词都是 唯一 的,并且以前按照一些自定义的顺序排序。
对 s 的字符进行置换,使其与排序的 order 相匹配。更具体地说,如果在 order 中的字符 x 出现字符 y 之前,那么在排列后的字符串中, x 也应该出现在 y 之前。
返回 满足这个性质的 s 的任意排列 。
示例 1:
输入: order = “cba”, s = “abcd”
输出: “cbad”
解释:
“a”、“b”、“c”是按顺序出现的,所以“a”、“b”、“c”的顺序应该是“c”、“b”、“a”。
因为“d”不是按顺序出现的,所以它可以在返回的字符串中的任何位置。“dcba”、“cdba”、“cbda”也是有效的输出。
示例 2:
输入: order = “cbafg”, s = “abcd”
输出: “cbad”
提示:
1 <= order.length <= 26
1 <= s.length <= 200
order 和 s 由小写英文字母组成
order 中的所有字符都 不同
java代码:
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();
}
}
版权声明
本文为[程序猿不脱发2]所创,转载请带上原文链接,感谢
https://blog.csdn.net/kangbin825/article/details/124362119
边栏推荐
- S2-062 remote command execution vulnerability recurrence (cve-2021-31805)
- 一个平面设计师的异想世界|ONES 人物
- SSL证书退款说明
- AI video cloud vs narrowband HD, who is the darling of the video era
- One way ANOVA of SPSS
- Uni app native app cloud packaging integrated Aurora push (jg-jpush) detailed tutorial
- 解锁OpenHarmony技术日!年度盛会,即将揭幕!
- leetcode:437. Path sum III [DFS selected or not selected?]
- 力扣刷题之完全二叉树的节点个数
- C set Logo Icon and shortcut icon
猜你喜欢

Fashion cloud learning - input attribute summary
![leetcode:437. Path sum III [DFS selected or not selected?]](/img/e7/d0983a3251dfaf2cf33b56a12ac149.png)
leetcode:437. Path sum III [DFS selected or not selected?]

梳理网络IP代理的几大用途

Dialogue with Bruce, author of PostgreSQL: "changing careers" is to better move forward

SQL exercise (I)

Use source insight to view and edit source code

Please help me see what this is, mysql5 5. Thanks

一个平面设计师的异想世界|ONES 人物

Kubernets Getting started tutoriel

Debug Jest test cases in VSCode, debug Jest test cases in VSCode, middle note basedir=$(dirname "$" (echo "$0" sed -e -e, s, \ \, / "-e").
随机推荐
Zero trust in network information security
S2-062 远程命令执行漏洞复现(cve-2021-31805)
SQL exercise (I)
【每日一题】棋盘问题
Labels and paths
Fashion cloud learning - input attribute summary
洛谷P5540 [BalkanOI2011] timeismoney | 最小乘积生成树 题解
Remote sensing image classification and recognition system based on convolutional neural network
风尚云网学习-input属性总结
Why is there a wrapper class? By the way, how to convert basic data types, wrapper classes and string classes?
One way ANOVA of SPSS
洛谷P3236 [HNOI2014]画框 题解
没有空闲服务器?导入 OVF 镜像快速体验 SmartX 超融合社区版
[wechat applet] Z-index is invalid
4.DRF 权限&访问频率&过滤&排序
QT double buffer drawing
BUUCTF WEB [GXYCTF2019]禁止套娃
【unity笔记】L4Unity中的基础光照
STM32 control stepper motor (ULN2003 + 28byj)
Ad20 supplementary note 3 - shortcut key + continuous update