File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ LeetCode Problem:409
3+ PROBLEM STATEMENT:
4+ 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.
5+
6+ Letters are case sensitive, for example, "Aa" is not considered a palindrome here.
7+ Input: s = "abccccdd"
8+ Output: 7
9+ Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
10+ Input: s = "a"
11+ Output: 1
12+ Explanation: The longest palindrome that can be built is "a", whose length is 1.
13+ Constraints:
14+ 1 <= s.length <= 2000
15+ s consists of lowercase and/or uppercase English letters only.
16+ */
17+ #include < iostream>
18+ #include < bits/stdc++.h>
19+ using namespace std ;
20+
21+ int solve (string &s){
22+ unordered_map<char ,int > mp;
23+ // count frequency of each character in string
24+ for (int i=0 ;i<s.length ();i++) mp[s[i]]++;
25+ int count=0 ;
26+ for (auto it:mp){
27+ // count the odd frequencies
28+ if (it.second %2 !=0 ) count++;
29+ }
30+ // Remove one char which appears for odd number of times
31+ if (count>1 )
32+ return s.length ()-count+1 ;
33+ return s.length ();
34+ }
35+
36+ int main (){
37+ string s;
38+ cin>>s;
39+ cout<<solve (s)<<endl;
40+ return 0 ;
41+ }
You can’t perform that action at this time.
0 commit comments