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 ...
随机推荐
- aipai服务架构
概述 业务服务器30+ 1.根据业务不同,有四个主入口,负责负载均衡. 2.主要是业务分离,防止宕机影响所有业务. 3.nginx反向代理,保证每个业务至少有两个服务. redis集群12台 主要使用 ...
- laravel5.8 IoC 容器
网上 对容器的解释有很多,这里只是记录,搬运! 1.简单理解: 2019-10-10 11:24:09 解析 lavarel 容器 IoC 容器 作用 就是 “解耦” .“依赖注入(DI) IoC 容 ...
- linux wireless 基础知识 MAC80211 CFG80211
转:http://blog.csdn.net/liuxd3000/article/details/23761663 1. 基本概念 • cfg80211: 用于对无线设备进行配置管理.与Full ...
- 使用busybox1.21.1制作根文件系统
1. 下载源码 https://busybox.net/downloads/ 2. 解压 3. 修改Makefile ~/busybox-1.21.1$ vi Makefile 164行: 修改前:C ...
- CSS属性(pading margin)
margin: margin:5px auto;意思上下为5,左右平均居中 margin-top: 20px; 上外边距 margin-right: 3 ...
- jmeter 压力测试tcp
cmd下管理员执行 jmeter 界面 英文版 中文切换以后不能执行 最多跑905个线程 线程限制 查看结果数 界面会跑死 windows环境 注意:测试1万的tcp并发连接 大爷的 window最 ...
- Docker 运行ELK日志监测系统,汉化Kibana界面
1.ELK日志监控简介 ELK由Elasticsearch.Logstash和Kibana三部分组件组成: Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引 ...
- POI导出Excel不弹出保存提示_通过ajax异步请求(post)到后台通过POI导出Excel
实现导出excel的思路是:前端通过ajax的post请求,到后台处理数据,然后把流文件响应到客户端,供客户端下载 文件下载方法如下: public static boolean downloadLo ...
- stm32之HAL串口中断的callback流程图
- 【agc004d】Teleporter
题目大意 一棵树,改变一些边的父亲,使得深度不超过k. 解题思路 我一开始就想到了贪心,结果莫名其妙的把这种方法给否决了, 然后考虑优化树形dp,然后优化失败⊙﹏⊙ 贪心思路很简单,也很好感受出来,从 ...