Skip to content

Commit dbce827

Browse files
committed
May 25
1 parent e4a2f2f commit dbce827

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from collections import Counter
2+
3+
4+
class Solution:
5+
def longestPalindrome(self, words: list[str]) -> int:
6+
freq = Counter(words)
7+
length, centered = 0, False
8+
9+
for word, count in freq.items():
10+
if word[0] == word[1]:
11+
if count % 2 == 0:
12+
length += count
13+
else:
14+
length += (count - 1)
15+
centered = True
16+
elif word[0] < word[1]:
17+
length += 2 * min(count, freq[word[::-1]])
18+
19+
length += centered
20+
return length * 2
21+
22+
23+
def main():
24+
words = ['lc', 'cl', 'gg']
25+
assert Solution().longestPalindrome(words) == 6
26+
27+
words = ['ab', 'ty', 'yt', 'lc', 'cl', 'ab']
28+
assert Solution().longestPalindrome(words) == 8
29+
30+
words = ['cc', 'll', 'xx']
31+
assert Solution().longestPalindrome(words) == 2
32+
33+
34+
if __name__ == '__main__':
35+
main()

2025-05-May-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
| May 22 | [3362. Zero Array Transformation III](https://leetcode.com/problems/zero-array-transformation-iii/) | Medium | Unsolved |
2929
| May 23 | [3068. Find the Maximum Sum of Node Values](https://leetcode.com/problems/find-the-maximum-sum-of-node-values/) | Hard | Unsolved |
3030
| May 24 | [2942. Find Words Containing Character](https://leetcode.com/problems/find-words-containing-character/) | Easy | Solved |
31-
| May 25 | []() | | |
31+
| May 25 | [2131. Longest Palindrome by Concatenating Two Letter Words](https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/) | Medium | Solved |
3232
| May 26 | []() | | |
3333
| May 27 | []() | | |
3434
| May 28 | []() | | |
@@ -41,5 +41,5 @@
4141
| Level | Problems | Solved | Unsolved |
4242
| --- | --- | --- | --- |
4343
| Easy | 6 | 6 | 0 |
44-
| Medium | 12 | 9 | 3 |
44+
| Medium | 13 | 10 | 3 |
4545
| Hard | 5 | 1 | 4 |

0 commit comments

Comments
 (0)