作者dont (dont)
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Mon Jan 13 19:24:34 2025
3223. Minimum Length of String After Operations
## 思路
先計算每個字元的出現次數
AAAAA --> AAA -> A
AAAA --> AA
如果奇數就+1, 偶數+2
## Code
```cpp
class Solution {
public:
int minimumLength(string s) {
int res = 0;
vector<int> count(26, 0);
for (char& ch: s) {
count[ch-'a']++;
}
for (int i=0; i<26; ++i) {
if (count[i] == 0)
continue;
res += (count[i] & 1) ? 1 : 2;
}
return res;
}
};
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 86.48.12.134 (日本)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1736767476.A.6E0.html
推 sustainer123: 大師 01/13 20:32
推 DJYOMIYAHINA: 大師 01/13 23:29