# 5. Longest Palindromic Substring - Medium

Given a string `s`, return *the longest* *palindromic* *substring* in `s`.

&#x20;

**Example 1:**

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

**Example 2:**

<pre><code><strong>Input: s = "cbbd"
</strong><strong>Output: "bb"
</strong></code></pre>

&#x20;

**Constraints:**

* `1 <= s.length <= 1000`
* `s` consist of only digits and English letters.

### Thoughts <a href="#thoughts" id="thoughts"></a>

1. This code is an implementation of a function from the Solution Class called longestPalindrome().
2. The function takes as input a string s and returns a string as output.
3. The first line in the function checks if the string is a palindrome. If it is, then it returns the string.
4. Then two variables: start and size are declared and initialized to 0 and 1 respectively.
5. There is a for loop where iterates through all the elements in the string one by one.
6. Inside the loop, two variables l and r are declared which are set to i minus size and i plus 1.
7. Two strings s1 and s2 are declared which is set to s\[l-1:r] and s\[l:r] respectively.
8. In the next line, it checks whether l-1 is greater than or equal to 0 and s1 is a palindrome. If that's the case, start and size are set to l-1 and size + 2 respectively.
9. If the above if-statement is false, it checks if s2 is a palindrome. If that's the case, start and size are set to l and size + 1 respectively.
10. Lastly, the function returns the substring of s starting at start with a length of size.
11. In the main function, there are two test cases which are used to test the function and they pass.

### Solutions <a href="#thoughts" id="thoughts"></a>

```
class Solution:
    def longestPalindrome(self, s: str) -> str:
        if s == s[::-1]: return s
        
        start, size = 0, 1
        for i in range(1, len(s)):
            l, r = i - size, i + 1
            s1, s2 = s[l-1:r], s[l:r]
            
            if l - 1 >= 0 and s1 == s1[::-1]:
                start, size = l - 1, size + 2
            elif s2 == s2[::-1]:
                start, size = l, size + 1
                
        return s[start:start+size]
        
def main():
    solution = Solution()
    
    # Test Cases
    assert solution.longestPalindrome("babad") == "bab"
    assert solution.longestPalindrome("cbbd") == "bb"
    
if __name__ == "__main__":
    main()

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://helfipangestu.gitbook.io/leetcode-solutions/solution-1-50/5.-longest-palindromic-substring-medium.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
