longest palindromic substring in O(n^^3)
0 Answers
longest palindromic substring in O(n^^3)
| Flag | PURGE
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
string s;
int l;
int chk(int a,int b)
{
if(a==b)
return 1;
while(b>a)
{
if(s[a]!=s[b])
{
return 0;
}
b--;
a++;
}
return 1;
}
int main()
{
cin>>s;
int maxl=0,sa,sb;
l=s.length();
for(int i=0;i<l;i++)
{
for(int j=i;j<l;j++)
{
int p=chk(i,j);
if(p==1);
{
if(j-i>=maxl)
{
sa=i;
sb=j;
maxl=j-i+1;
}
}
}
}
for(int h=sa;h<=sb;h++)
printf("%c",s[h]);
}
i am getting absurd results :(
please help to point any mistake in the code?
Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.