---
Difficulty: Medium
Source: 160 Days of Problem Solving
Tags:
- Dynamic Programming
- Strings
- palindrome
---
# 🚀 _Day 5. Longest Palindrome in a String_ 🧠
The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/batch/gfg-160-problems/track/dynamic-programming-gfg-160/problem/longest-palindrome-in-a-string3411)
## 💡 **Problem Description:**
Given a string `s`, your task is to find the **longest palindromic substring** within `s`.
- A **substring** is a contiguous sequence of characters within the string (i.e., `s[i...j]` for `0 ≤ i ≤ j < len(s)`).
- A **palindrome** reads the same forwards and backwards, i.e., `reverse(s) == s`.
- If there are multiple palindromic substrings of the same maximum length, **return the first occurrence** from left to right.
## 🔍 **Example Walkthrough:**
### **Example 1:**
#### **Input:**
```
s = "forgeeksskeegfor"
```
#### **Output:**
```
"geeksskeeg"
```
#### **Explanation:**
Possible palindromic substrings include `"kssk"`, `"ss"`, `"eeksskee"`, etc. However, **"geeksskeeg"** is the **longest** among them.
### **Example 2:**
#### **Input:**
```
s = "Geeks"
```
#### **Output:**
```
"ee"
```
#### **Explanation:**
The substring **"ee"** is the longest palindrome in `"Geeks"`.
### **Example 3:**
#### **Input:**
```
s = "abc"
```
#### **Output:**
```
"a"
```
#### **Explanation:**
All substrings `"a"`, `"b"`, and `"c"` have length 1, which is the maximum. Returning the first occurrence results in `"a"`.
### **Constraints:**
- $\(1 \leq s.\text{size()} \leq 10^3\)$
- `s` consists of only **lowercase English letters**.
## 🎯 **My Approach:**
### **Expand Around Center**
1. A **palindrome** can be expanded from its **center**.
2. For each index `i` in the string:
- Consider two scenarios for the center:
1. **Odd-length palindromes** (center at `i`).
2. **Even-length palindromes** (center between `i` and `i+1`).
3. Expand outwards while **left and right characters match**.
4. **Track the maximum length palindrome** found and its start position.
This approach checks each possible center in `O(N)` and potentially expands in `O(N)`, leading to `O(N²)` overall time.
## 🕒 **Time and Auxiliary Space Complexity**
- **Expected Time Complexity:** **O(N²)**, because for each index we can expand in both directions.
- **Expected Auxiliary Space Complexity:** **O(1)**, since we use only a few extra variables for tracking indices and lengths.
## 📝 **Solution Code**
## **Code (C++)**
```cpp
class Solution {
public:
string longestPalindrome(string &s) {
int n = s.size(), start = 0, maxLen = 0;
for (int i = 0; i < n; i++) {
for (int l : {i, i + 1}) {
int j = i;
while (j >= 0 && l < n && s[j] == s[l]) j--, l++;
if (l - j - 1 > maxLen) start = j + 1, maxLen = l - j - 1;
}
}
return s.substr(start, maxLen);
}
};
```
## **2️⃣ Dynamic Programming Approach (O(N²) Time, O(N²) Space)**
#### **Algorithm Steps:**
1. Create a **2D DP table** `dp[i][j]`, where `dp[i][j]` is `true` if `s[i:j]` is a palindrome.
2. Set `dp[i][i] = true` (single-character substrings are palindromes).
3. If two adjacent characters are equal (`s[i] == s[i+1]`), set `dp[i][i+1] = true`.
4. For substrings of length 3 or more, use the formula:
- `dp[i][j] = (s[i] == s[j] && dp[i+1][j-1])`
5. Keep track of the longest palindrome found and return it.
```cpp
class Solution {
public:
string longestPalindrome(string &s) {
string t = "#";
for (char c : s) t += c, t += "#";
int n = t.size(), center = 0, right = 0, maxLen = 0, start = 0;
vector⚡ Alternative Approaches