Skip to content

Commit 87da63a

Browse files
solves longest palindrome in python
1 parent 52ec501 commit 87da63a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/SumOfLeftLeaves.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/sum_of_left_leaves.py) |
111111
| 405 | [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/ConvertNumberToHexadecimal.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/convert_a_number_to_hexadecimal.py) |
112112
| 408 | 🔒 [Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation) | Easy | |
113-
| 409 | [Longest Palindrome](https://leetcode.com/problems/longest-palindrome) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/LongestPalindrome.java) |
113+
| 409 | [Longest Palindrome](https://leetcode.com/problems/longest-palindrome) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/LongestPalindrome.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/longest_palindrome.py) |
114114
| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/FizzBuzz.java) |
115115
| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/ThirdMaximumNumber.java) |
116116
| 415 | [Add Strings](https://leetcode.com/problems/add-strings) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/AddString.java) |

python/longest_palindrome.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def longestPalindrome(self, s: str) -> int:
3+
character_frequency = {}
4+
for character in s:
5+
character_frequency[character] = character_frequency.get(character, 0) + 1
6+
length = 0
7+
had_odd_length = False
8+
for frequency in character_frequency.values():
9+
if frequency % 2 == 0:
10+
length += frequency
11+
else:
12+
length += frequency - 1
13+
had_odd_length = True
14+
return length + int(had_odd_length)

0 commit comments

Comments
 (0)