We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent dccc9eb commit 6215243Copy full SHA for 6215243
solution.cpp
@@ -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