Longest Palindrome - Complete Solution Guide
Longest Palindrome is LeetCode problem 409, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a string 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. Example 1: Input: s = "abccccdd" Output: 7 Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7. Example 2: Input: s = "a" Output: 1 Explanation: The longest palindrome that can be built is "a", whose length is 1. Constraints: 1 <= s.lengt
Detailed Explanation
The problem asks us to find the length of the longest palindrome that can be constructed from a given string 's'. The string 's' consists of lowercase or uppercase letters, and case sensitivity matters (e.g., 'A' and 'a' are different). A palindrome is a string that reads the same forwards and backward. The goal is to rearrange the letters in 's' (or use only some of them) to form the longest possible palindrome, and return its length. The input string's length is between 1 and 2000.
Solution Approach
The solution uses a hash map (or array in the C implementation) to count the frequency of each character in the input string. It then iterates through these frequencies. For each character, if its count is even, we add the entire count to the total length of the palindrome. If the count is odd, we add (count - 1) to the total length, effectively pairing up all possible instances of that character while leaving one instance unused *unless* it can be used as the center character. Finally, if we encountered at least one character with an odd count, we increment the total length by 1 to account for using one of those characters as the palindrome's center.
Step-by-Step Algorithm
- Step 1: Create a hash map (or array) to store the frequency of each character in the input string 's'.
- Step 2: Iterate through the input string 's', and update the frequency count for each character in the hash map (or array).
- Step 3: Initialize a variable 'length' to 0. This variable will store the length of the longest palindrome.
- Step 4: Initialize a boolean variable 'odd_exists' (or an integer 'oddCount') to false (or 0). This will track if there is an odd number frequency count
- Step 5: Iterate through the frequency counts in the hash map (or array).
- Step 6: For each frequency count, if it's even, add the count to 'length'.
- Step 7: If the frequency count is odd, add (count - 1) to 'length' and set 'odd_exists' to true (or increment 'oddCount' to 1).
- Step 8: After iterating through all frequency counts, if 'odd_exists' is true (or 'oddCount' is greater than 0), increment 'length' by 1.
- Step 9: Return 'length'.
Key Insights
- Insight 1: A palindrome can be formed by pairing up characters that appear an even number of times. For example, 'aabbcc' can form 'abccba'.
- Insight 2: If there's an odd number of occurrences for some characters, we can use all but one of each of these characters to build the palindrome's outer layers.
- Insight 3: At most one character with an odd count can be placed in the center of the palindrome to maximize the length.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Hash Table, String, Greedy.
Companies
Asked at: Akamai, HP, Walmart Labs.