zoukankan      html  css  js  c++  java
  • 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(26, pair<char,int>('a',0));
            for (int i = 0; i < s.size(); i++) {
                int idx = s[i] - 'a';
                if (map[idx].second == 0) {
                    map[idx].first = s[i];
                    map[idx].second = 1;
                }
                else {
                    map[idx].second++;
                }
            }
            sort(map.begin(), map.end(), cmp);
            //for(auto m : map) cout << m.first << m.second << endl;
            int idx = map.size() - 1;
            int maxrep = map[idx].second;
            string ret = "";
            while (idx >= 0 && map[idx].second == maxrep) {
                string tmpstr = string(1,map[idx].first);
                ret += tmpstr;
                idx--;
            }
            //cout << ret << endl;
            vector<string> retvec(map.back().second - 1, ret);
            int cnt = 0;
            while (idx >= 0 && map[idx].second != 0) {
                int tmp = idx;
                string tmpstr =string(1, map[tmp].first);
                while (map[tmp].second != 0) {
                    retvec[cnt] += tmpstr;
                    map[tmp].second--;
                    cnt++;
                    if(cnt >= retvec.size()) cnt = 0;
                }
                idx--;
            }
            for (auto s : retvec) {
                if (s.size() < k) return "";
            }
            string finalret = "";
            for (auto s : retvec) finalret += s;
            finalret += ret;
            return finalret;
        }
    };
  • 相关阅读:
    老罗锤子手机发布会,我感到深深地愧疚!
    微价值:专访《甜心爱消除》的个人开发者Lee,日入千元
    [个人开发者赚钱二]从自己最熟悉的方面入手,获取小利
    [个人开发者赚钱一]改变思维,从心开始
    个人开发者赚钱一、改变思维,从心开始
    OC中的点语法,成员变量的作用域
    OC self super isa指针
    OC面向对象多态笔记
    OC面向对象继承关系和组合关系笔记
    OC面向对象封装
  • 原文地址:https://www.cnblogs.com/ethanhong/p/10162560.html
Copyright © 2011-2022 走看看