-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path409_Longest_Palindrome.cpp
More file actions
50 lines (48 loc) · 918 Bytes
/
409_Longest_Palindrome.cpp
File metadata and controls
50 lines (48 loc) · 918 Bytes
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
#include"struct_define.h"
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include <unordered_map>
using namespace std;
static const auto x=[](){
std::ios::sync_with_stdio(false);
std:cin.tie(nullptr);
return nullptr;
}();
class Solution
{
public:
int longestPalindrome(string s)
{
unordered_map<char,int> charcnt;
int cnt = 0;
for(auto c : s)
{
charcnt[c]++;
}
bool addone=true;
for(auto ele:charcnt)
{
if(ele.second % 2 != 0)
{
if(addone)
{
cnt += ele.second;
addone=false;
}
else cnt += (ele.second-1);
}
else
cnt += ele.second;
}
return cnt;
}
};
int main(int argc, char const *argv[])
{
Solution sol;
cout<<sol.longestPalindrome("abccccdd");
cout<<sol.longestPalindrome("bb");
return 0;
}