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 ...
随机推荐
- OWASP Hakcing Lab在线漏洞环境
OWASP Hakcing Lab在线漏洞环境 OWASP hakcing-lab 是一个提供免费的远程安全(Web)挑战和 OWASP TOP 10,OWASP WebGoat,OWASP Ha ...
- ubuntu16.04下NVIDIA GTX965M显卡驱动PPA安装
禁用nouveau驱动 Ubuntu系统集成的显卡驱动程序是nouveau,我们需要先将nouveau从linux内核卸载掉才能安装NVIDIA官方驱动.将nouveau添加到黑名单blacklist ...
- Selenium3-浏览器与驱动对照
在学selenium自动化测试时,遇到的第一个大问题便是浏览器版本.浏览器驱动版本与selenium的版本不对应,而无法驱动浏览器进行自动化操作. 收集了网上的一些技术文档,决定也整理一份相对较全面的 ...
- dyld: Symbol not found: _OBJC_CLASS_$_xxxx 错误闪退
dyld: Symbol not found: _OBJC_CLASS_$_xxx 引起的APP闪退可以先查看xxx所属的库,然后将其设为optional 例如dyld: Symbol not fou ...
- CentOS7 安装 RocketMQ 实践和小示例
CentOS7 安装 RocketMQ 实践和小示例 1.通过 SSH 工具(比如 XShell)连接到 CentOS7 服务器上: 2.进入到 /usr/local 目录中: cd /usr/loc ...
- 转:ubuntu添加用户adduser,并给予sudo权限
ubuntu添加用户adduser,并给予sudo权限 如何创建ubuntu新用户? 首先打开终端,输入:sudo adduser username正在添加用户“username”... 正在添加新组 ...
- vue项目搭建和开发流程 vue项目配置ElementUI、jQuery和Bootstrap环境
目录 一.VUE项目的搭建 1. 环境搭建 2. 项目的创建和启动 二. 开发项目 1. 配置vue项目启动功能 2. 开发vue项目 (1)项目文件的作用 (2)vue项目开发流程 (3)vue项目 ...
- Linux 常用命令备忘
安装wget 方便联网下载: centos : sudo yum -y install wget 安装vim : yum -y install vim* set nu ...
- jQuery 3.0 的新特性
1. jQuery 3.0 运行在严格模式下 当下几乎支持jQuery 3.0的浏览器都支持严格模式,该版本正是基于此进行编译发布的. 你的代码已经运行在非严格模式?不用担心,你无需重写.jQuery ...
- Blade 模板
在Laravel 5.3中,@foreach指令提供了更加强大的功能,在每一个@foreach循环体中都可以调用一个新的$loop变量.该变量是一个stdClass实例,包含了当前循环的元数据信息,让 ...