Longest Palindromic Substring in TypeScript

function longestPalindrome(s: string): string {
    // Helper function to expand around the potential center.
    function expandAroundCenter(left: number, right: number): string {
        while (left >= 0 && right < s.length && s[left] === s[right]) {
            left--;
            right++;
        }
        // Return the palindrome substring.
        return s.substring(left + 1, right);
    }

    let longest: string = '';

    for (let i = 0; i < s.length; i++) {
        // Check for odd-length palindromes (single center character).
        const oddPalindrome: string = expandAroundCenter(i, i);
        if (oddPalindrome.length > longest.length) {
            longest = oddPalindrome;
        }

        // Check for even-length palindromes (two center characters).
        const evenPalindrome: string = expandAroundCenter(i, i + 1);
        if (evenPalindrome.length > longest.length) {
            longest = evenPalindrome;
        }
    }

    return longest;
}

Next