-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest_palindrome.cpp
65 lines (60 loc) · 1.46 KB
/
longest_palindrome.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* =====================================================================================
*
* Filename: longest_palindrome.cpp
*
* Description: 409. Longest Palindrome.
* https://leetcode.com/problems/longest-palindrome/
*
* Version: 1.0
* Created: 02/16/2023 15:19:24
* Revision: none
* Compiler: gcc
*
* Author: [email protected]
* Organization:
*
* =====================================================================================
*/
#include <array>
#include <string>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::array;
using std::string;
class Solution {
public:
int longestPalindrome(string s) {
array<int, 54> counts = {0};
for (char c : s) {
if (c >= 'A' && c <= 'Z') {
counts[c - 'A']++;
} else if (c >= 'a' && c <= 'z') {
counts[c - 'a' + 26]++;
}
}
int len = 0;
bool odd = false;
for (int c : counts) {
len += c & (~0x01);
if ((c & 0x01) != 0) {
odd = true;
}
}
if (odd) {
len++;
}
return len;
}
};
TEST(Solution, longestPalindrome) {
std::vector<std::pair<string, int>> cases = {
std::make_pair(string("a"), 1),
std::make_pair(string("ccc"), 3),
std::make_pair(string("abccccdd"), 7),
std::make_pair(string("aaaAaaaa"), 7),
};
for (auto& c : cases) {
EXPECT_EQ(Solution().longestPalindrome(c.first), c.second);
}
}