-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0409.LongestPalindrome.cpp
More file actions
40 lines (34 loc) · 1.05 KB
/
0409.LongestPalindrome.cpp
File metadata and controls
40 lines (34 loc) · 1.05 KB
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
class Solution {
public:
unsigned int characterCounts[26 * 2];
int longestPalindrome(string s) {
// Speed thingies.
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// Clear memory.
memset(characterCounts, 0, sizeof(characterCounts));
// Create character count infromation.
const int n = s.size();
for (int i = 0; i < n; i++) {
const char c = s[i];
if ('a' <= c && c <= 'z') characterCounts[c - 'a']++;
else if ('A' <= c && c <= 'Z') characterCounts[(c - 'A') + 26]++;
}
// Count max palindrome length.
int maxPalinDrome = 0;
// Add all double characters onto count.
for (int i = 0; i < sizeof(characterCounts) / sizeof(*characterCounts); i++) {
const int mod = characterCounts[i] % 2;
maxPalinDrome += characterCounts[i] - mod;
characterCounts[i] = mod;
}
// Check for palindrome center (odd character count).
for (int i = 0; i < sizeof(characterCounts) / sizeof(*characterCounts); i++) {
if (characterCounts[i] != 1) continue;
maxPalinDrome++;
break;
}
return maxPalinDrome;
}
};