-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec3_LongestPalandroone.cpp
More file actions
53 lines (43 loc) · 983 Bytes
/
Copy pathlec3_LongestPalandroone.cpp
File metadata and controls
53 lines (43 loc) · 983 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
51
52
53
https://leetcode.com/problems/longest-palindrome/description/
class Solution {
public:
int longestPalindrome(string s) {
vector<int> lower(26 , 0);
vector<int>upper(26 , 0 );
for( int i = 0 ; i<s.size();i++)
{
if(s[i]>='a')
{
lower[ s[i] - 'a']++;
}
else
{
upper[s[i]-'A']++;
}
}
int count = 0 ;
bool odd = 0 ;
for(int i = 0 ; i<26 ; i++)
{
if(lower[i] %2 ==0)
{
count+= lower[i];
}
else
{
odd = 1;
count += lower[i]-1;
}
if(upper[i]%2==0)
{
count+=upper[i];
}
else
{
count+=upper[i]-1;
odd =1 ;
}
}
return odd +count;
}
};