This is one of those problems where the clean idea matters more than the amount of code. For 1400. Construct K Palindrome Strings, the solution in this repository is mainly a bit manipulation solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: bit manipulation.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are canConstruct, count.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N), space : O(1)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 316 ms, faster than 42.86% of C++ online submissions for Construct K Palindrome Strings.
02//Memory Usage: 12.2 MB, less than 100.00% of C++ online submissions for Construct K Palindrome Strings.
03class Solution {
04public:
05 bool canConstruct(string s, int k) {
06 if(s.size() < k) return false;
07 if(s.size() == k) return true;
08 map<char, int> count;
09
10 for(char c : s){
11 count[c]++;
12 }
13
14 int odd = 0;
15 for(auto it = count.begin(); it != count.end(); it++){
16 if(it->second % 2 == 1){
17 odd++;
18 }
19 }
20 return odd <= k;
21 }
22};
23
24//speed up with vector
25//Runtime: 88 ms, faster than 42.86% of C++ online submissions for Construct K Palindrome Strings.
26//Memory Usage: 11.8 MB, less than 100.00% of C++ online submissions for Construct K Palindrome Strings.
27class Solution {
28public:
29 bool canConstruct(string s, int k) {
30 if(s.size() < k) return false;
31 if(s.size() == k) return true;
32 vector<int> count(26);
33
34 for(char c : s){
35 count[c-'a']++;
36 }
37
38 int odd = 0;
39 for(int i = 0; i < 26; i++){
40 if(count[i] % 2 == 1){
41 odd++;
42 }
43 }
44 return odd <= k;
45 }
46};
47
48//bitset
49//https://leetcode.com/problems/construct-k-palindrome-strings/discuss/563379/JavaC%2B%2BPython-Straight-Forward
50//Runtime: 80 ms, faster than 42.86% of C++ online submissions for Construct K Palindrome Strings.
51//Memory Usage: 12 MB, less than 100.00% of C++ online submissions for Construct K Palindrome Strings.
52//time: O(N), space : O(1)
53class Solution {
54public:
55 bool canConstruct(string s, int k) {
56 bitset<26> odd;
57
58 for(char c : s){
59 odd.flip(c-'a');
60 }
61
62 return odd.count() <= k && k <= s.size();
63 }
64};
Cost