-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday30.cpp
More file actions
63 lines (50 loc) · 1.43 KB
/
day30.cpp
File metadata and controls
63 lines (50 loc) · 1.43 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
using namespace std;
// Function to calculate the maximum palindrome length
vector<int> maxPalindromeLengths(const vector<string>& testCases) {
vector<int> results;
for (const string& s : testCases) {
unordered_map<char, int> charCount;
// Count frequencies of each character
for (char c : s) {
charCount[c]++;
}
int palindromeLength = 0;
bool oddFound = false;
// Calculate the maximum palindrome length
for (const auto& pair : charCount) {
int count = pair.second;
if (count % 2 == 0) {
palindromeLength += count;
} else {
palindromeLength += count - 1;
oddFound = true;
}
}
// Add one if an odd count exists
if (oddFound) {
palindromeLength += 1;
}
results.push_back(palindromeLength);
}
return results;
}
int main() {
int T;
cin >> T;
vector<string> testCases(T);
// Input the test cases
for (int i = 0; i < T; ++i) {
cin >> testCases[i];
}
// Get the results
vector<int> results = maxPalindromeLengths(testCases);
// Output the results
for (int res : results) {
cout << res << endl;
}
return 0;
}