Longest Palindrome

 

  1. Clarify the problem:

    • The problem requires finding the length of the longest palindromic substring within a given string.
    • We need to implement a function that takes a string as input and returns an integer representing the length of the longest palindromic substring.
  2. Analyze the problem:

    • Input: A string.
    • Output: An integer representing the length of the longest palindromic substring.
    • Constraints:
      • The input string may be empty.
      • The input string may contain uppercase and lowercase letters, digits, and special characters.
      • The problem asks for a substring, which means the characters of the palindrome must be contiguous.
  3. Design an algorithm:

    • We can solve this problem using the dynamic programming approach.
    • We need to create a 2D array, dp, of size (n, n), where n is the length of the input string.
    • The value dp[i][j] will represent whether the substring from index i to j is a palindrome.
    • We can fill in the values of dp by iterating over the string from the last character to the first character.
    • To determine if a substring from index i to j is a palindrome, we check if the characters at indices i and j are equal and if the substring between i+1 and j-1 is a palindrome.
    • We initialize the diagonal elements of dp as True since single characters are palindromes.
    • We iterate over the string, starting from the last character, and for each character, iterate over the remaining characters after it.
    • If the current characters are equal and the substring between them is a palindrome, we set dp[i][j] as True.
    • We keep track of the longest palindromic substring encountered so far and update it whenever we find a longer palindrome.
    • Finally, we return the length of the longest palindromic substring.
  4. Explain your approach:

    • We will implement a function called longestPalindrome that takes a string as input.
    • We initialize n as the length of the string.
    • We create a 2D array, dp, of size (n, n), and initialize all elements as False.
    • We set the diagonal elements of dp as True since single characters are palindromes.
    • We initialize max_length as 1, representing the length of the longest palindromic substring encountered so far.
    • We iterate over the string from the last character to the first character using the variable i.
    • Inside the outer loop, we iterate over the characters after i using the variable j.
    • If the current characters at indices i and j are equal and the substring between them is a palindrome (dp[i+1][j-1] is True), we set dp[i][j] as True.
    • If dp[i][j] is True, we update max_length to be the maximum of max_length and j - i + 1, representing the length of the current palindromic substring.
    • Finally, we return max_length as the length of the longest palindromic substring.
  5. Write clean and readable code:

    python
  6. def longestPalindrome(s): n = len(s) dp = [[False] * n for _ in range(n)] max_length = 1 # Initialize diagonal elements as True for i in range(n): dp[i][i] = True # Iterate over the string for i in range(n - 1, -1, -1): for j in range(i + 1, n): if s[i] == s[j] and (j - i == 1 or dp[i + 1][j - 1]): dp[i][j] = True max_length = max(max_length, j - i + 1) return max_length
  7. Test your code:

    python
  8. # Test case 1: Example case s = "babad" # The longest palindromic substring is "bab" or "aba". # Both substrings have a length of 3. print(longestPalindrome(s)) # Output: 3 # Test case 2: Empty string s = "" # The input string is empty, so the longest palindromic substring is an empty string. print(longestPalindrome(s)) # Output: 0 # Test case 3: Single character s = "a" # The input string has only one character, which is a palindrome itself. print(longestPalindrome(s)) # Output: 1 # Test case 4: All characters are the same s = "bbb" # The longest palindromic substring is "bbb". # The length of the substring is 3. print(longestPalindrome(s)) # Output: 3 # Test case 5: String with multiple palindromic substrings s = "cbbd" # The longest palindromic substring is "bb". # The length of the substring is 2. print(longestPalindrome(s)) # Output: 2
  9. Optimize if necessary:

    • The solution using dynamic programming has a time complexity of O(n^2), where n is the length of the input string. We need to fill in the dp table of size (n, n) and each cell takes constant time to calculate.
    • The space complexity is also O(n^2) since we use a dp table of size (n, n).
  10. Handle error cases:

    • We need to consider the case where the input string is empty. In this case, we can simply return 0 as the length of the longest palindromic substring.
  11. Discuss complexity analysis:

    • Let n be the length of the input string.
    • The time complexity of the solution is O(n^2) since we iterate over the string twice.
    • The space complexity is O(n^2) since we use a dp table of size (n, n).
Next Post Previous Post