Skip to content

Commit 7829d49

Browse files
Update longest_palindrome.py
1 parent b63199f commit 7829d49

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

longest_palindrome.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
1-
from collections import Counter
2-
class Solution:
3-
def longestPalindrome(self, s: str) -> int:
4-
hashed = dict(Counter(s))
5-
sz, szz = 1, len(s)
6-
for ele in hashed.values():
7-
if ele % 2 == 0:
8-
sz += (ele -1)
1+
# from collections import Counter
2+
# class Solution:
3+
# def longestPalindrome(self, s: str) -> int:
4+
# hashed = dict(Counter(s))
5+
# sz, szz = 1, len(s)
6+
# for ele in hashed.values():
7+
# if ele % 2 == 0:
8+
# sz += (ele -1)
9+
# else:
10+
# sz += ele
11+
# if sz > szz:
12+
# return szz
13+
# else:
14+
# return sz
15+
class Solution(object):
16+
from collections import Counter
17+
def longestPalindrome(self, s):
18+
m = set()
19+
count = 0
20+
for c in s:
21+
if c in m:
22+
m.remove(c)
23+
count += 1
924
else:
10-
sz += ele
11-
if sz > szz:
12-
return szz
13-
else:
14-
return sz
25+
m.add(c)
26+
return count * 2 + 1 if len(m) != 0 else count * 2
27+
28+
29+

0 commit comments

Comments
 (0)