Re: [閒聊] 每日leetcode
1400. Construct K Palindrome Strings
## 思路
1.字串長度必須要大於K
2.回文: AABB / ABA
計算字元的奇偶個數 最多只能有K個字元是奇數個
## CODE
```CPP
class Solution {
public:
bool canConstruct(string s, int k) {
if (k > s.length())
return false;
vector<int> count(26, 0);
for (char& ch: s) {
++count[ch-'a'];
}
for (int i=0; i<26; ++i) {
k -= (count[i] & 1);
}
return k >= 0;
}
};
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 194.195.89.62 (日本)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1736586438.A.801.html
討論串 (同標題文章)
完整討論串 (本文為第 1262 之 1554 篇):