File tree Expand file tree Collapse file tree 1 file changed +12
-15
lines changed
2237-longest-palindrome-by-concatenating-two-letter-words Expand file tree Collapse file tree 1 file changed +12
-15
lines changed Original file line number Diff line number Diff line change 5
5
class Solution :
6
6
def longestPalindrome (self , words : List [str ]) -> int :
7
7
count = Counter (words )
8
- answer = 0
8
+ ans = 0
9
9
central = False
10
-
10
+
11
11
for word , word_count in count .items ():
12
-
13
- # if the word is palindrome
12
+ # If the word is palindrome
14
13
if word [0 ] == word [1 ]:
15
- if word_count % 2 == 0 :
16
- answer += word_count
14
+ if word_count % 2 == 0 :
15
+ ans += word_count
17
16
else :
18
- answer += word_count - 1
17
+ ans += word_count - 1
19
18
central = True
20
-
21
- else :
22
- answer += min (word_count , count [word [1 ] + word [0 ]])
23
-
19
+ elif word [0 ] < word [1 ]:
20
+ ans += 2 * min (word_count , count [word [1 ] + word [0 ]])
21
+
24
22
if central :
25
- answer += 1
26
-
27
- return 2 * answer
28
-
23
+ ans += 1
24
+
25
+ return 2 * ans
You can’t perform that action at this time.
0 commit comments