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 55class Solution :
66 def longestPalindrome (self , words : List [str ]) -> int :
77 count = Counter (words )
8- answer = 0
8+ ans = 0
99 central = False
10-
10+
1111 for word , word_count in count .items ():
12-
13- # if the word is palindrome
12+ # If the word is palindrome
1413 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
1716 else :
18- answer += word_count - 1
17+ ans += word_count - 1
1918 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+
2422 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