LC 358. Rearrange String k Distance Apart
Given a non-empty string s and an integer k, rearrange the string such that the same characters are at least distance k from each other.
All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empty string "".
Example 1:
Input: s = "aabbcc", k = 3
Output: "abcabc"
Explanation: The same letters are at least distance 3 from each other.
Example 2:
Input: s = "aaabc", k = 3
Output: ""
Explanation: It is not possible to rearrange the string.
Example 3:
Input: s = "aaadbbcc", k = 2
Output: "abacabcd"
Explanation: The same letters are at least distance 2 from each other.
Runtime: 8 ms, faster than 99.59% of C++ online submissions for Rearrange String k Distance Apart.
和之前的一道题很类似
bool cmp(pair<char, int> a, pair<char, int> b) {
if (a.second != b.second) return a.second < b.second;
return a.first < b.first;
}
class Solution {
public:
string rearrangeString(string s, int k) {
vector<pair<char, int>> map(, pair<char,int>('a',));
for (int i = ; i < s.size(); i++) {
int idx = s[i] - 'a';
if (map[idx].second == ) {
map[idx].first = s[i];
map[idx].second = ;
}
else {
map[idx].second++;
}
}
sort(map.begin(), map.end(), cmp);
//for(auto m : map) cout << m.first << m.second << endl;
int idx = map.size() - ;
int maxrep = map[idx].second;
string ret = "";
while (idx >= && map[idx].second == maxrep) {
string tmpstr = string(,map[idx].first);
ret += tmpstr;
idx--;
}
//cout << ret << endl;
vector<string> retvec(map.back().second - , ret);
int cnt = ;
while (idx >= && map[idx].second != ) {
int tmp = idx;
string tmpstr =string(, map[tmp].first);
while (map[tmp].second != ) {
retvec[cnt] += tmpstr;
map[tmp].second--;
cnt++;
if(cnt >= retvec.size()) cnt = ;
}
idx--;
}
for (auto s : retvec) {
if (s.size() < k) return "";
}
string finalret = "";
for (auto s : retvec) finalret += s;
finalret += ret;
return finalret;
}
};
LC 358. Rearrange String k Distance Apart的更多相关文章
- 358. Rearrange String k Distance Apart
/* * 358. Rearrange String k Distance Apart * 2016-7-14 by Mingyang */ public String rearrangeString ...
- LeetCode 358. Rearrange String k Distance Apart
原题链接在这里:https://leetcode.com/problems/rearrange-string-k-distance-apart/description/ 题目: Given a non ...
- [LeetCode] 358. Rearrange String k Distance Apart 按距离k间隔重排字符串
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
- 【LeetCode】358. Rearrange String k Distance Apart 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rearrang ...
- [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
- Leetcode: Rearrange String k Distance Apart
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
- [Swift]LeetCode358. 按距离为k隔离重排字符串 $ Rearrange String k Distance Apart
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
- LC 394. Decode String
问题描述 Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], wh ...
- [LC] 1048. Longest String Chain
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predece ...
随机推荐
- C获取数组长度
c语言中,定义数组后可以用sizeof命令获得数组的长度(可容纳元素个数) 如: { int data[4]; int length; length=sizeof(data)/sizeof(data[ ...
- STM32输出比较模式
搜索好久,各种文章良莠不齐,转载以下几篇 http://www.eeworld.com.cn/mcu/article_2016101130334.html(输出比较冻结模式) http://www.e ...
- three.js之正投影摄像机与透视投影摄像机的区别
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- C++归并排序(数组&链表)
1.归并排序(Merge Sort) 归并排序的性能不受输入数据的影响,始终都是O(n log n)的时间复杂度.代价是需要额外的内存空间. 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采 ...
- Java中的equals,==,compareTo和compare的比较
Java中的equals(),==,compareTo()和compare() 首先只有==可以用作两个基本类型数据之间的比较,当然是值比较.当用作两个对象比较时,比较的是对象引用,而不是值比较. 其 ...
- Wuss Weapp 微信小程序 UI 组件库
微信小程序 UI 组件库 Github地址 https://github.com/phonycode/wuss-weapp 文档 https://phonycode.github.io/wuss-we ...
- 使用idea 搭建一个 SpringBoot + Mybatis + logback 的maven 项目
(注意项目名不能有大写......),把项目类型 改成 War 类型.(web项目) 使用 mybatis-generator 插件 生成 实体类 和 接口 在 resources 目录 中 新建一个 ...
- PHP中使用PDO的预处理功能避免SQL注入
不使用预处理功能 <?php $id = $_GET['id']; $dsn = 'mysql:host=localhost;port=3306;dbname=database'; try { ...
- 题解 小B的询问
题面 解析 这就是道莫队模板啊啊!! 因此,似乎并没有什么好讲的. 莫队算法传送门 我们只需要将询问存下来, 离线处理就行了. 还是上代码吧: #include<bits/stdc++.h> ...
- webuploader如何判断是否上传的是空文件?
在'beforeFileQueued'事件中可以判断: // 当有文件被添加进队列的时候 uploader.on( 'beforeFileQueued', function( file ) { if( ...