Longest Palindromic Substring

Key Idea

Solution

class Solution:
    def longestPalindrome(self, s: str) -> str:
        # If the string is empty or has one character,
        # it is already the longest palindrome
        if len(s) <= 1:
            return s

        # Store the longest palindrome found so far
        max_palindromic = ""

        # Try every character as a possible center
        for current_pointer, character in enumerate(s):

            # ==================================================
            # Odd case
            # Example: "bab"
            #
            # The center is one character:
            # b a b
            #   ^
            #
            # So both pointers start at the same index
            # ==================================================
            left_pointer = current_pointer
            right_pointer = current_pointer

            # Expand while left and right are inside the string
            while left_pointer >= 0 and right_pointer < len(s):

                # If both sides match, we found a palindrome
                if s[left_pointer] == s[right_pointer]:

                    # Save the current palindrome substring
                    current_palindromic = s[left_pointer:right_pointer + 1]

                    # Expand outward
                    left_pointer -= 1
                    right_pointer += 1

                # If they do not match, stop expanding
                else:
                    break

            # Update answer if this odd palindrome is longer
            if len(current_palindromic) > len(max_palindromic):
                max_palindromic = current_palindromic

            # ==================================================
            # Even case
            # Example: "bb"
            #
            # The center is between two characters:
            # b b
            # ^ ^
            #
            # So left starts at current index,
            # right starts at the next index
            # ==================================================
            left_pointer = current_pointer
            right_pointer = current_pointer + 1

            # Important:
            # reset current_palindromic for the even case
            current_palindromic = ""

            # Expand while left and right are inside the string
            while left_pointer >= 0 and right_pointer < len(s):

                # If both sides match, we found a palindrome
                if s[left_pointer] == s[right_pointer]:

                    # Save the current palindrome substring
                    current_palindromic = s[left_pointer:right_pointer + 1]

                    # Expand outward
                    left_pointer -= 1
                    right_pointer += 1

                # If they do not match, stop expanding
                else:
                    break

            # Update answer if this even palindrome is longer
            if len(current_palindromic) > len(max_palindromic):
                max_palindromic = current_palindromic

        return max_palindromic

Complexity