当前位置:网站首页>358. K distance interval rearrange string sorting
358. K distance interval rearrange string sorting
2022-06-29 07:35:00 【Empress Yu】
358. K The distance interval rearranges the string
Give you a non empty string
sAnd an integerk, You have to put this stringsRearrange the letters in , Make the position spacing of the same letters in the rearranged string At least byk. If you can't do it , Please return an empty string"".Example 1:
Input : s = "aabbcc", k = 3 Output : "abcabc" explain : The same letters are spaced at least in the new string 3 Unit distance .Example 2:
Input : s = "aaabc", k = 3 Output : "" explain : There is no way to find possible rearrangement results .Example 3:
Input : s = "aaadbbcc", k = 2 Output : "abacabcd" explain : The same letters are spaced at least in the new string 2 Unit distance .Tips :
1 <= s.length <= 3 * 10^5sIt's only made up of lowercase letters0 <= k <= s.length
The result of doing the question
success , Priority queue , Sliding windows are useless , Pure sort + Array + aggregate ( It's OK not to , Just plain arrays )
Method : Sort
1. There is a length of n String , Every time k Divide into groups , It can be divided out part=(n+k-1) Group , If there are more than characters part, Must not form a template string , Go straight back to
2. Letters are counted in an array , Record the number of each character
3. Longer than 0 Put into the list , Reverse by quantity
4. Take as many as possible each time k The characters of , Put in the answer
5. Need to check the index position , If it is too close to the front , Then try the same number of other letters
6. Make a sequence each time ( Only the length of the 26, So it doesn't matter if you've been waiting )
class Solution {
public String rearrangeString(String s, int k) {
if(k==0) return s;
int[] arr = new int[26];
int n = s.length();
int part = (n+k-1)/k;// The maximum number of each part
for(int i = 0; i < n; i++){
int index = s.charAt(i)-'a';
arr[index]++;
if(arr[index]>part){
return "";
}
}
// In reverse order of quantity
List<int[]> list = new ArrayList<>();
for(int i = 0; i < 26; i++){
if(arr[i]>0){
list.add(new int[]{i,arr[i]});// Letter , Number
}
}
// Reverse by quantity
list.sort((a,b)->b[1]-a[1]);
int[] lastId = new int[26];// Last index bit
Arrays.fill(lastId,-1);
StringBuilder sb = new StringBuilder();
boolean move = true;
for(int i = 0; i < n&&move; ){
move = false;
// From more to less
for (int[] data : list) {
// Jump out if you don't score
if (data[1] == 0) break;
int id = sb.length();
// Not far enough , skip
if(lastId[data[0]]!=-1 && id-lastId[data[0]]<k)
continue;
// This round has been operated
move = true;
lastId[data[0]] = id;
sb.append((char) (data[0] + 'a'));
--data[1];
++i;
// Gather together k This round ends
if (sb.length()%k==0) {
break;
}
}
// Quantity change , Rearrange
list.sort((a,b)->b[1]-a[1]);
}
return sb.length()==n?sb.toString():"";
}
}边栏推荐
- 解题-->在线OJ(十三)
- 什么是测试架构师
- KingbaseES 中select distinct on 语句
- Mmclassification installation and debugging
- Deploy Prometheus server service system management
- [popular science materials] materials from scientific spirit to scientific knowledge
- About the problem that the kingbasees temporary file is too large
- Schnuka: 3D machine vision inspection system 3D vision inspection application industry
- 施努卡:3d机器视觉检测系统 3d视觉检测应用行业
- Digital IC Design - UART
猜你喜欢

E-commerce is popular, how to improve the store conversion rate?

Relevance - correlation analysis

施努卡:什么是视觉定位系统 视觉定位系统的工作原理

利用IPv6实现公网访问远程桌面

机器学习笔记 - 时间序列使用机器学习进行预测

Utilisation d'IPv6 pour réaliser l'accès public au bureau distant
如何看待软件测试培训?你需要培训吗?

Appium automation test foundation ADB common commands (III)

项目中 if else 的代替写法
![[translation] E-Cloud. Large scale CDN using kubeedge](/img/ac/178c078589bb5bc16dbdc8f4ae9525.png)
[translation] E-Cloud. Large scale CDN using kubeedge
随机推荐
Select distinct on statement in kingbasees
Markdown 技能树(2):段落及强调
When the soft keyboard appears, it makes my EditText field lose focus
九州云助力内蒙古“东数西算”工程,驱动测绘行业智慧新生态
Markdown skill tree (8): code blocks
关于开发web场景下如何解决手机访问web跨域问题
Unexpected exception ... code: Badrequest when downloading Xilinx 2018.2
Markdown skill tree (9): tables
National Security Agency and CISA kubernetes reinforcement guidelines - new content in version 1.1
详解Autosar Arxml中的CANFD报文及格式
Who is the main body of the waiting insurance record? Record in the local network security, right?
About the problem that the kingbasees temporary file is too large
Es query syntax
施努卡:3D视觉识别系统 3D视觉检测原理
Markdown 技能树(1):MarkDown介绍
Comment choisir les fournisseurs de marques CRM dans l'industrie du vêtement?
Blue Bridge Cup -- Analysis of the second batch of test questions of the 13th session
tf. count_ nonzero
感知健康生活 赋能无界连接 ——为OpenHarmony 3.1生态构建贡献芯海力量
[qnx hypervisor 2.2 user manual]6.2.1 communication between guests