Skip to content

Commit 05837cc

Browse files
authored
Create Longest Palindrome by Concatenating Two Letter Words.py
1 parent 1fd9204 commit 05837cc

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#2131. Longest Palindrome by Concatenating Two Letter Words
2+
class Solution:
3+
def longestPalindrome(self, words: List[str]) -> int:
4+
alphabet_size = 26
5+
count = [[0 for j in range(alphabet_size)] for i in range(alphabet_size)]
6+
for word in words:
7+
count[ord(word[0]) - ord('a')][ord(word[1]) - ord('a')] += 1
8+
answer = 0
9+
central = False
10+
for i in range(alphabet_size):
11+
if count[i][i] % 2 == 0:
12+
answer += count[i][i]
13+
else:
14+
answer += count[i][i] - 1
15+
central = True
16+
for j in range(i + 1, alphabet_size):
17+
answer += 2 * min(count[i][j], count[j][i])
18+
if central:
19+
answer += 1
20+
return 2 * answer

0 commit comments

Comments
 (0)