-
Notifications
You must be signed in to change notification settings - Fork 0
/
409.longest-palindrome.cpp
81 lines (79 loc) · 1.65 KB
/
409.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* @lc app=leetcode id=409 lang=cpp
*
* [409] Longest Palindrome
*
* https://leetcode.com/problems/longest-palindrome/description/
*
* algorithms
* Easy (53.80%)
* Likes: 2996
* Dislikes: 170
* Total Accepted: 304.1K
* Total Submissions: 563K
* Testcase Example: '"abccccdd"'
*
* Given a string s which consists of lowercase or uppercase letters, return
* the length of the longest palindrome that can be built with those letters.
*
* Letters are case sensitive, for example, "Aa" is not considered a palindrome
* here.
*
*
* Example 1:
*
*
* Input: s = "abccccdd"
* Output: 7
* Explanation: One longest palindrome that can be built is "dccaccd", whose
* length is 7.
*
*
* Example 2:
*
*
* Input: s = "a"
* Output: 1
* Explanation: The longest palindrome that can be built is "a", whose length
* is 1.
*
*
*
* Constraints:
*
*
* 1 <= s.length <= 2000
* s consists of lowercase and/or uppercase English letters only.
*
*
*/
// @lc code=start
class Solution {
public:
int longestPalindrome(string s) {
int size = s.size();
int ans = 0;
if(size ==1){
return 1;
}
unordered_map<char,int> stringhold;
for(char c:s){
if (stringhold.count(c)){
ans++;
stringhold.erase(c);
}else{
stringhold[c] = 1;
}
// cout<<c<<ans<<endl;
}
ans *=2;
/*if(size%2 ==1){
ans++;
}*/
for (auto it = stringhold.begin();it!=stringhold.end();it++){
return ans+1;
}
return ans;
}
};
// @lc code=end