当前位置:网站首页>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
边栏推荐
- Keyword interpretation and some APIs in RT thread
- Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
- STM32控制步进电机(ULN2003+28byj)
- 【微信小程序】z-index失效
- QT interprocess communication
- Everything can be expected in the future | one 2022 campus recruitment officially opened
- Xinwangda announced that the price of battery products had been increased, and the investment of "weixiaoli" exceeded 1 billion
- Plato farm - a game of farm metauniverse with Plato as the goal
- QT one process runs another
- Idea setting copyright information
猜你喜欢
万事有你 未来可期 | ONES 2022校园招聘正式开启
Fastjson 2 is coming, the performance continues to improve, and it can fight for another ten years
Xinwangda announced that the price of battery products had been increased, and the investment of "weixiaoli" exceeded 1 billion
AI video cloud vs narrowband HD, who is the darling of the video era
Zigbee之CC2530最小系统及寄存器配置(1)
【每日一题】棋盘问题
box-sizing
Introduction to metalama 4 Use fabric to manipulate items or namespaces
Qt绘制图像
Debug Jest test cases in VSCode, debug Jest test cases in VSCode, middle note basedir=$(dirname "$" (echo "$0" sed -e -e, s, \ \, / "-e").
随机推荐
S2-062 远程命令执行漏洞复现(cve-2021-31805)
c# 设置logo图标和快捷方式的图标
Metalama简介4.使用Fabric操作项目或命名空间
How to solve the computer system card?
Recommended programming AIDS: picture tool snipaste
One way ANOVA of SPSS
RT-thread中关键词解释及部分API
Idea setting copyright information
Everything can be expected in the future | one 2022 campus recruitment officially opened
梳理网络IP代理的几大用途
Web17——EL与JSTL的使用
电脑系统卡如何解决?
解决disagrees about version of symbol device_create
4.DRF 权限&访问频率&过滤&排序
php生成json处理中文
航芯技术分享 | ACM32 MCU安全特性概述
QT one process runs another
SQL exercise (I)
Jiachen chapter Genesis "inner universe" joint Edition
Introduction to metalama 4 Use fabric to manipulate items or namespaces