Skip to content

Commit 11882be

Browse files
committed
Sync LeetCode submission Runtime - 54 ms (66.43%), Memory - 35.7 MB (12.27%)
1 parent a0a9f5d commit 11882be

File tree

1 file changed

+12
-15
lines changed
  • 2237-longest-palindrome-by-concatenating-two-letter-words

1 file changed

+12
-15
lines changed

2237-longest-palindrome-by-concatenating-two-letter-words/solution.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,21 @@
55
class 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

0 commit comments

Comments
 (0)