code for longest palindromic substring

Example 1:

Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.

Example 2:

Input: s = "cbbd"
Output: "bb"
pull/896/head
Gaurav Saha 2022-10-03 00:05:17 +05:30 committed by GitHub
parent 62907b69ec
commit 0b6bfd864a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,50 @@
class Solution
{
public:
string longestPalindrome(string s)
{
int n=s.size();
//dp[i][j] will be false if substring s[i..j] is not palindrome, Else true
vector<vector<bool>> dp(n, vector<bool> (n,false));
//All substrings of length 1 are palindromes
for(int i=0; i<n; i++) dp[i][i]=true;
int start=0, lenMax=1;
// check for sub-string of length 2.
for(int i=0; i<n-1; i++)
{
if(s[i]==s[i+1])
{
dp[i][i+1] = true;
start = i;
lenMax = 2;
}
}
// Check for sub-string of length greater than 2.
for(int k=3; k<=n; k++) // k is length of substring
{
for(int i=0; i<n-k+1; i++) // Fix the starting index
{
int j=i+k-1; // Get the ending index of substring from starting index i and length k
// checking for sub-string from ith index to jth index if s[i+1] to s[j-1] is a palindrome
if(dp[i+1][j-1]==true && s[i]==s[j])
{
dp[i][j] = true;
if(k>lenMax)
{
lenMax = k;
start=i;
}
}
}
}
return s.substr(start, lenMax); //print sub-string from start to lenMax
}
};