LeetCode 3223. Minimum Length of String After Operations

35 阅读1分钟

🔗 leetcode.com/problems/mi…

题目

  • 给一个字符串,进行如下操作,返回经过操作后,字符串的最小长度
  • 挑选三个一样的字符组成的三元组,删除左边和右边的

思路

  • 模拟

代码

class Solution {
public:
    int minimumLength(string s) {
        unordered_map<char, int> mp;
        for (auto ch : s) {
            mp[ch]++;
        }
        int ans = 0;
        for (auto pair : mp) {
            char ch = pair.first;
            int freq = pair.second;
            while (freq >= 3) {
                freq -= 2;
            }
            ans += freq;
        }
        
        return ans;
        
    }
};