<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;babad&quot;
<strong>Output:</strong> &quot;bab&quot;
<strong>Explanation:</strong> &quot;aba&quot; is also a valid answer.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;cbbd&quot;
<strong>Output:</strong> &quot;bb&quot;
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 1000</code></li>
	<li><code>s</code> consist of only digits and English letters.</li>
</ul>

class Solution:
    def longestPalindrome(self, s: str) -> str:
        # we can get a palindrome for odd num char if symmetric with center char
        # we can get a palindrome for even num char if two middle char are same

        # brute force:
        # generate all possible substrings then determine which of these are the largest palindrome

        # frame as DP problem:
        # overall problem: find the longest palindrome in s
        # subproblem: which characters should we include to get a palindrome
            # to not distrupt current palindrome --> we must add two char at a time to both ends --> only add if both are the same
            # if they are not the same, we need to change the center of the palindrome
        # pseudo code:
            # first start at the center of the string
            # expand initial substring to include all repeating characters --> if all char are the same, it is a palindrome
            # we consider the characters on both sides of the current sub --> if they are the same, add them to sub
            # repeat
            # if next char are not the same OR we run out of char, start over again considering char to left and right of center as beginning of palindrome

        longest_s = ""
        # we don't want to recompute possible substrings we've seen already
        # store endpoints of starting substring
        memo = set()
        def helper(center, prev):
            nonlocal longest_s, memo
            # base case
            if center < 0 or center >= len(s):
                return

            l, r = center, center

            # want to expand substring to include all consecutive same char
            while l-1 > 0 and s[l-1] == s[center]:
                l -= 1
            while r+1 < len(s) and s[r+1] == s[center]:
                r += 1

            # find longest substring from center
            if (l, r) not in memo:
                # store starting sub to memo
                memo.add((l, r))

                while l-1 >= 0 and r+1 < len(s) and s[l-1] == s[r+1]:
                    l -= 1
                    r += 1

                # get substring that is a palindrome
                sub = s[l:r+1]
                if len(sub) > len(longest_s):
                    longest_s = sub

            # repeat process considering other indicies as center
            if prev and prev == center + 1:
                helper(center-1, center)
            elif prev and prev == center - 1:
                helper(center+1, center)
            else:
                helper(center-1, center)
                helper(center+1, center)

        helper(len(s)//2, None)
        return longest_s

18100