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.length <= 2000s consists of lowercase and/or uppercase English letters only.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the longest palindrome, a brute force strategy means we check every possible section of the given text. We'll look at each section, one by one, and see if it reads the same forwards and backward. We remember the longest one we've found so far.
Here's how the algorithm would work step-by-step:
def longest_palindrome_brute_force(input_string):
longest_palindrome = ""
for start_index in range(len(input_string)):
for end_index in range(start_index, len(input_string)):
# Consider all possible substrings
substring = input_string[start_index:end_index + 1]
# Check if substring is a palindrome
if substring == substring[::-1]:
# Update longest palindrome if current is longer
if len(substring) > len(longest_palindrome):
longest_palindrome = substring
return longest_palindromeThe most efficient way to find the longest palindrome involves expanding outwards from potential centers. Instead of checking every possible substring, we strategically examine areas that are more likely to be palindromes. This avoids redundant checks and quickly identifies the longest one.
Here's how the algorithm would work step-by-step:
def longestPalindrome(input_string):
longest_palindrome_so_far = ""
string_length = len(input_string)
for i in range(string_length):
# Check for odd length palindromes.
left_index = i
right_index = i
# Expand around the center as long as possible.
while left_index >= 0 and right_index < string_length and input_string[left_index] == input_string[right_index]:
current_palindrome_length = right_index - left_index + 1
if current_palindrome_length > len(longest_palindrome_so_far):
longest_palindrome_so_far = input_string[left_index:right_index + 1]
left_index -= 1
right_index += 1
# Check for even length palindromes.
left_index = i
right_index = i + 1
# Need to check even palindromes
while left_index >= 0 and right_index < string_length and input_string[left_index] == input_string[right_index]:
current_palindrome_length = right_index - left_index + 1
if current_palindrome_length > len(longest_palindrome_so_far):
longest_palindrome_so_far = input_string[left_index:right_index + 1]
left_index -= 1
right_index += 1
# Return the longest palindrome found
return longest_palindrome_so_far| Case | How to Handle |
|---|---|
| Null or empty input string | Return an empty string as an empty string is a palindrome of length 0 |
| Single character string | Return the single character string itself, as it is a palindrome |
| String with two identical characters (e.g., 'aa') | Return the entire string as it is a palindrome |
| String with two different characters (e.g., 'ab') | Return the first character, as a single character is always a palindrome |
| String with all identical characters (e.g., 'aaaa') | Return the entire string, as it is a palindrome |
| Very long string to check for time complexity (e.g., 10000 'a's) | Dynamic programming approach or expanding around center provides optimal time complexity for large inputs. |
| String contains non-alphanumeric characters | Preprocess the string to remove non-alphanumeric characters or handle them based on problem's requirements. |
| String contains mixed case letters | Consider converting the entire string to lowercase before processing to handle case-insensitive palindromes. |