Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bbead9b

Browse files
authored
Create Longest_Palindrome.py
1 parent 0b9d38f commit bbead9b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
ans += i
16+
return ans

0 commit comments

Comments
 (0)