JZX轻语:简

LeetCode 2716 - 最小化字符串长度

发表于2025年04月05日

#哈希表

挺简单的题目,按题目的操作,字符串中所出现的每个字母最终都会减少到1个,所以我们只需要统计字符串中不同字母的数量即可。

class Solution {
public:
    int minimizedStringLength(string s) {
        vector<int> cnt_map(26, 0);
        for (const auto& ch : s) {
            cnt_map[ch - 'a']++;
        }
        int res = 0;
        for (int i = 0; i < 26; ++i) res += cnt_map[i] > 0;
        return res;
    }
};

闪念标签:LC

题目链接:https://leetcode.cn/problems/minimize-string-length/