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

# 5. Longest Palindromic Substring

## Problem Statement

<br>

Given a string `s`, return *the longest*&#x20;

*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.

## Intuition

```
Rather coming from edges 
Expand from the centre

This will eliminate the extra loop 

N^2
```

### Links

<https://leetcode.com/problems/longest-palindromic-substring/description/>

### Video Links

<https://www.youtube.com/watch?v=XYQecbcd6_c&t=275s&ab_channel=NeetCode>

### Approach 1:

```
Scanning from centre
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    string ans = "";
    void expand(string &s , int left ,int right){

        while(left >= 0 &&  right < s.size()){
            if(s[left] != s[right])
                break;
            left--,right++;
        }

        if(ans.size() < right - left )
            ans = s.substr(left + 1 , right - left - 1);
    }

    string longestPalindrome(string s) {

        for(int i = 0 ; i < s.size() ; i++){
            expand(s , i , i);
            expand(s , i , i+1);
        }
        
        return ans;
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://coding-9.gitbook.io/untitled/string/medium/5.-longest-palindromic-substring.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.
