Back to photostream

Longest Palindromic Substring LeetCode Solution

Problem Statement

The Longest Palindromic Substring LeetCode Solution - “Longest Palindromic Substring” states that You are Given a string s, return the longest palindromic substring in s.

 

Note: A palindrome is a word that reads the same backward as forwards, e.g. madam.

Example:

 

 

s = "babad"

"bab"

Explanation:

 

All the unique palindromic substrings are: "b", "a", "d", "bab", "aba".

 

Out of these, “bab” and “aba” are the longest substrings.

s = "cbbd"

"bb"

Explanation:

 

All the unique palindromic substrings are: "c", "b", "d", "bb".

 

Out of these, “bb” is the longest substring.

Brute Force Solution

Idea:

We can check all the substrings and check which substrings are palindrome, then take the longest among them.

Code:

C++ Program of Longest Palindromic Substring LeetCode Solution

#include

using namespace std;

 

bool check(string &s, int i, int j)

{

while (i max_len)

{

max_len = j - i + 1;

starting_index = i;

}

}

}

}

return s.substr(starting_index, max_len);

}

int main()

{

string s = "babad";

cout

 

www.tutorialcup.com/leetcode-solutions/longest-palindromi...

54 views
0 faves
0 comments
Uploaded on April 16, 2022