Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions longestPalindromicSubstring.cpptringFunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Solution {
public:


string longestPalindrome(string s) {
int n=s.length();
vector<vector<bool>>dp(n, vector<bool>(n));

for(int i=n-1;i>=0;i--)
{
for(int j=i;j<n;j++)
{
if(i==j)
{
dp[i][j] = true;
}
else if(s[i]==s[j])
{
dp[i][j] = (i+1<j-1)?dp[i+1][j-1]:true;
}
else
dp[i][j] =0;
}
}
int maxx = 0;
int ind = -1;
int jnd = -1;

for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(dp[i][j] && (j-i+1)>maxx)
{
maxx = j-i+1;
ind = i;
jnd = j;
}
}
}

cout<<ind<<" "<<jnd<<endl;
//return "";
return string(s.begin()+ind, s.begin()+jnd+1);
//return string(s.begin()+ind , s.begin() + ind+ maxx);
}
};