> For the complete documentation index, see [llms.txt](https://adit0503.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://adit0503.gitbook.io/leetcode/5.-longest-palindromic-substring.md).

# 5. Longest Palindromic Substring

Medium

Given a string **s**, find the longest palindromic substring in **s**. You may assume that the maximum length of **s** is 1000.

## :innocent: [Solution](https://leetcode.com/problems/longest-palindromic-substring/)

{% tabs %}
{% tab title="O(n3)" %}

```python
class Solution:
    
    def isPalindrome(s):
        return s == s[::-1]
     
    def longestPalindrome(self, s: str) -> str:
        
        max_length = 0
        ans = ''
        
        for i in range(len(s)):
            for j in range(i+1, len(s)+1):
                temp = s[i:j]
                if Solution.isPalindrome(temp) and len(temp) > max_length:
                    max_length = len(temp)
                    ans = temp
        return ans
        
```

{% endtab %}
{% endtabs %}
