Leetcode: Longest Palindromic Substring, Python3

103 / 103 test cases passed
Status: Accepted
Runtime: 3732 ms (O(n2) solution)
Memory Usage: 12.8 MB

class Solution:
    def longestPalindrome(self, s: str) -> str:
        if len(s) <= 1: return s

        for i in range(len(s), 0, -1):         # i: length of palendrome to check for
            for j in range(0, len(s) - i + 1): # j: starting position within s to check
                test = s[j:j+i]
                rev = test[::-1]
                if test == rev:
                    return test

More from Joshua
All posts