Skip to content

Commit 6215243

Browse files
committed
Add C++ solution of problem #1400
1 parent dccc9eb commit 6215243

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

solution.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// math (referenced from LeetCode Hint in this problem)
2+
// Count the # characters with odd counts then check
3+
// whether it is less than or equal to k.
4+
// Edge case: if s.length < k then just return false.
5+
6+
class Solution {
7+
public:
8+
bool canConstruct(string s, int k) {
9+
if(s.size() < k)
10+
return false;
11+
12+
vector<int> cnt(26);
13+
for(char c : s)
14+
cnt[c - 'a']++;
15+
int odd_cnt = 0;
16+
for(int i = 0; i < 26; i++)
17+
odd_cnt += (cnt[i] % 2);
18+
return odd_cnt <= k;
19+
}
20+
};

0 commit comments

Comments
 (0)