We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0b9d38f commit bbead9bCopy full SHA for bbead9b
Leetcode_30day_challenge/Longest_Palindrome.py
@@ -0,0 +1,16 @@
1
+# Solution - 1 | Time: O(N) and Space: O(N)
2
+from collections import Counter
3
+class Solution:
4
+ def longestPalindrome(self, s: str) -> int:
5
+ c = Counter(s)
6
+ ans = 0
7
+
8
+ for i in c.values():
9
+ if ans % 2 == 1: # If odd
10
+ if i % 2 == 0: # If i is even, then it can be added
11
+ ans += i
12
+ else: # If i is odd, then (odd-1) which is even should be added
13
+ ans += i-1
14
+ else: #If even, either odd or even can be added.
15
16
+ return ans
0 commit comments