forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
45 lines (31 loc) · 888 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/// Source : https://leetcode.com/problems/construct-k-palindrome-strings/
/// Author : liuyubobobo
/// Time : 2020-04-04
#include <iostream>
#include <vector>
using namespace std;
/// Using Hash Map
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
bool canConstruct(string s, int k) {
if(s.size() < k) return false;
if(s.size() == k) return true;
vector<int> freq(26, 0);
for(char c: s) freq[c - 'a'] ++;
int odd = 0;
for(int e: freq) if(e % 2) odd ++;
while(odd && k) k --, odd --;
return odd < 1;
}
};
int main() {
cout << Solution().canConstruct("annabelle", 2) << endl;
// true
cout << Solution().canConstruct("leetcode", 3) << endl;
// false
cout << Solution().canConstruct("abcdefghijklmnopqrstuvwxyz", 25) << endl;
// false
return 0;
}