Skip to content

Commit 73f6209

Browse files
authored
Create longest-palindrome.cpp
1 parent 84b82d5 commit 73f6209

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

C++/longest-palindrome.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int longestPalindrome(string s) {
7+
int odds = 0;
8+
for (auto c = 'A'; c <= 'z'; ++c) {
9+
odds += count(s.cbegin(), s.cend(), c) & 1;
10+
}
11+
return s.length() - odds + (odds > 0);
12+
}
13+
};

0 commit comments

Comments
 (0)