aboutsummaryrefslogtreecommitdiff
path: root/longest_palindrome.py
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /longest_palindrome.py
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'longest_palindrome.py')
-rw-r--r--longest_palindrome.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/longest_palindrome.py b/longest_palindrome.py
new file mode 100644
index 0000000..58cd217
--- /dev/null
+++ b/longest_palindrome.py
@@ -0,0 +1,34 @@
+# 409. Longest Palindrome
+
+"""
+given a sting 's' which consists of lowercase or uppercase letters, return
+the length of the longest palindrome that can be built with those letters.
+letters are case sensitive for example "Aa" is not considered a palindrome
+"""
+
+
+class Solution(object):
+ def longestPalindrome(self, s):
+ """
+ :type s: str
+ :rtype: int
+ """
+ odd_cnt, d = 0, {}
+ for c in s:
+ if c in d:
+ d[c] += 1
+ else:
+ d[c] = 1
+ if d[c] % 2 == 1:
+ odd_cnt += 1
+ else:
+ odd_cnt -= 1
+ if odd_cnt > 1:
+ return len(s) - odd_cnt + 1
+ return len(s)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.longestPalindrome("abccccdd"))
+ print(obj.longestPalindrome("a"))