You are not logged in. Please login at to post your questions!


Plzz help me solve this :)?


A string(small alphabets) is given. You can swap any two alphabets ONLY ONCE and u have to give the max length of a palindromic substring possible.

Here is my code for your reference(It gives wrong answer for "chiaki") :



using namespace std; int palindrome(string s) {

int l=s.size();string temp=s; for(int i=0;i<=l/2;i++)




if(temp==s){return 1;}

else{return 0;}


int search(string s)


int lmax=1;

for(int i=0;i<s.size();i++)


for(int j=1;j<=s.size()-i;j++)


if(palindrome(s.substr(i,j)) && lmax<=j){lmax=j;}



return lmax;


int main()


string s;

cin>>s; string temp=s;int lmax=1;

for(int i=0;i<s.size();i++)

{ s=temp;

for(int j=0;j<s.size();j++)


swap(s[i],s[j]);int l=search(s);



} cout<<endl<<endl<<lmax;

return 0; }

asked 06 Dec, 00:16

chaitu12789's gravatar image

accept rate: 0%

Your Palindrome function won't work in string length 2 case.

As you'll swap both the times and would result in the same number as before. So it'll always show palindrome in length 2 case. Just remove the "=" sign in the loop.

Remaining seems to be correct except the final printing statement. I think mostly the problem won't ask for printing the answer after 2 new lines. Remove those 2 endls and print directly.

Also, to optimize the code you can change your O(n^3) complexity to O(n^2) to calculate the longest palindromic substring.

Provide the Question link if it still doesn't work.


answered 06 Dec, 01:36

black_truce's gravatar image

accept rate: 38%

Thanks i got my mistake :)

(07 Dec, 13:15) chaitu127890★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 06 Dec, 00:16

question was seen: 80 times

last updated: 07 Dec, 13:15