跳转至

进行操作使字符串为空⚓︎

Leetcode题目链接

描述⚓︎

详见中文题目链接

解答⚓︎

class Solution {
public:
    string lastNonEmptyString(string s) {
        vector<int> count(26), last(26);
        for (int i = 0; i < s.size(); i++) {
            int offset = s[i] - 'a';
            count[offset]++;
            last[offset] = i;
        }

        vector<int> ids;
        int mx = *max_element(count.begin(), count.end());
        for (int i = 0; i < 26; i++) {
            if (count[i] == mx) {
                ids.push_back(last[i]);
            }
        }

        sort(ids.begin(), ids.end());

        string t(ids.size(), 0);
        for (int i = 0; i < ids.size(); i++) {
            t[i] = s[ids[i]];
        }
        return t;
    }
};