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 ...
 
随机推荐
- 【2】Zookeeper安装
			
一.环境准备 Linux操作系统 Java运行环境(1.6或以上) 服务器列表: 配置主机名映射. vi /etc/hosts ##添加如下内容 168.5.7.75 server1 168.5.7. ...
 - Linux服务器开发:工具
			
预处理 将所有#defined删除,并且展开 处理所有条件预处理指令 处理#include,将被包含的文件插入到该预编译指令的位置 过滤所有的//./**/ 保留所有#pragma编译指令 编译 词法 ...
 - Linux sed命令 -- 三剑客老二
			
格式: sed [OPTION]... {script-only-if-no-other-script} [input-file]... sed [OPTION]... ‘地址定界+[高级]编辑命令’ ...
 - 【转】草根老师的 linux字符设备驱动详解
			
Linux 驱动 之 模块化编程 Linux 驱动之模块参数和符号导出 Linux 设备驱动之字符设备(一) Linux 设备驱动之字符设备(二) Linux 设备驱动之字符设备(三)
 - Spring boot传值注意事项
			
后台debug看到有获取到这个字段的值了,但就是传到前端后,就丢失了这个userId字段,觉得非常奇怪,想不通 后来看到 @JsonIgnore 这个注解就知道原因了 共同学习,共同进步,若有补充,欢 ...
 - [uboot] (番外篇)uboot串口&console&stdio设备工作流程 (转)
			
[uboot] uboot流程系列:[project X] tiny210(s5pv210)上电启动流程(BL0-BL2)[project X] tiny210(s5pv210)从存储设备加载代码到D ...
 - 【洛谷P2387】魔法森林
			
题目大意:给定一个 N 个点,M 条边的无向图,边有两个边权 a, b,求从 1 号节点到 N 号节点路径的两个权值和的最大值最小是多少. 题解: 对于有两个属性的结构的最优化问题,可以考虑先按照其中 ...
 - java 和 c#返回值方法
			
java 和 c#都是应用很广泛的语言,也互有优劣. 这两者都是面向对象的语言,在一个方法中如果类型不是void那么是需要return一个返回值的. 但是如果想要返回多个值该怎么办? 排除直接返回一个 ...
 - POJ-2752-Seek the Name(KMP, 循环节)
			
链接: https://vjudge.net/problem/POJ-2752#author=0 题意: 给定若干只含小写字母的字符串(这些字符串总长≤400000),在每个字符串中求出所有既是前缀又 ...
 - gtid同步异常处理
			
gtid同步异常处理 分析出现问题时候GTID值 通过分析法获取gtid值 通过查看mysql> show slave status \G;查看一下信息并记录下来:Retrieved_Gtid_ ...