Skip to content

Commit 8726366

Browse files
authored
Create longest-palindrome-by-concatenating-two-letter-words.cpp.py
1 parent 841ac13 commit 8726366

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
import collections
5+
6+
7+
class Solution(object):
8+
def longestPalindrome(self, words):
9+
"""
10+
:type words: List[str]
11+
:rtype: int
12+
"""
13+
cnt = collections.Counter(words)
14+
result = remain = 0
15+
for x, c in cnt.iteritems():
16+
if x == x[::-1]:
17+
result += c//2
18+
remain |= c%2
19+
elif x < x[::-1] and x[::-1] in cnt:
20+
result += min(c, cnt[x[::-1]])
21+
return result*4+remain*2

0 commit comments

Comments
 (0)