当前位置:网站首页>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
边栏推荐
- BUUCTF WEB [GXYCTF2019]禁止套娃
- How does sqlserver insert or update the number of weeks of the day instead of text
- Object. The disorder of key value array after keys
- 梳理網絡IP代理的幾大用途
- Lesson 24 analysis of classical problems
- 一个平面设计师的异想世界|ONES 人物
- Idea setting copyright information
- Debug Jest test cases in VSCode, debug Jest test cases in VSCode, middle note basedir=$(dirname "$" (echo "$0" sed -e -e, s, \ \, / "-e").
- One way ANOVA of SPSS
- MySQL函数-递归函数
猜你喜欢

Object. The disorder of key value array after keys

Basic software testing Day2 - Case Execution

SSM框架系列——注解开发day2-2

Unlock openharmony technology day! The annual event is about to open!

Remote sensing image classification and recognition system based on convolutional neural network

How do traditional enterprises cope with digital transformation? These books give you the answer

免费试用一个月的服务器,并附上教程

XinChaCha Trust SSL Organization Validated

SSL certificate refund instructions

bert-base-chinese下载(智取)
随机推荐
Lesson 23 temporary objects
Source code analysis of synchronousqueue
[daily question] chessboard question
Web17 -- use of El and JSTL
BUUCTF WEB [BJDCTF2020]ZJCTF,不过如此
【蓝桥杯】4月17日省赛刷题训练(前3道题)
[vulnhub range] - DC2
Image attribute of input: type attribute of fashion cloud learning -h5
大家帮我看一下这是啥情况,MySQL5.5的。谢了
Lesson 25 static member variables of classes
Basic software testing Day2 - Case Execution
S2-062 remote command execution vulnerability recurrence (cve-2021-31805)
免费试用一个月的服务器,并附上教程
Object. The disorder of key value array after keys
云原生KubeSphere部署Mysql
解锁OpenHarmony技术日!年度盛会,即将揭幕!
The continuous construction of the Internet industry platform is not only able to collect traffic
Plato Farm-以柏拉图为目标的农场元宇宙游戏
NBIOT的AT指令
甲辰篇 創世紀《「內元宇宙」聯載》