-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlongestPalindrome.cpp
More file actions
29 lines (22 loc) · 1.43 KB
/
longestPalindrome.cpp
File metadata and controls
29 lines (22 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
class Solution {
public:
int longestPalindrome(string s) {
unordered_set<char> set; // Equivalent to HashSet in Java. Used to store values in
// a Hash table.
int len = 0; // length of the resulting palindrome
for(char i : s){ //A for-each loop where i will take the value of each character
// in the string s.
if(set.find(i) == set.end()) //if element is not present in the set, insert the
set.insert(i); // element in the set.
else{ //If element is already present in the set, we know that this
len += 2; // is the second time this element is present in s, so it will
set.erase(i); // contribute to the resulting palindrome. Length of the
} // palindrome is increased by 2, and the element is removed.
}
if(set.empty() == false) // As we get an EVEN length of palindrome, we can still add a
len += 1; // character in the centre and it will still be a palindrome.
// So, if HashSet is not empty, that means it has a single
// char left, which can contribute to length of palindrome.
return len; //Return the length of resulting palindrome.
}
};