HTML & JS
#include<bits/stdc++.h>
using namespace std;
map<string,int>done;
 long int minimumhelp(string &s)
{   
    int size=s.size();
	if(size<2)
	return 0;
	if(done.find(s)!=done.end())
	return done[s];
	else
	{
	if(s[0]==s[size-1])
	{
	string operate1=s.substr(1,size-2);
	return  done[s]=minimumhelp(operate1);
   }
	else if(s[0]!=s[size-1])
	{
	string operate2=s.substr(0,size-1);
	string operate3=s.substr(1,size-1);
	
	return done[s]=1+ min(minimumhelp(operate2),minimumhelp(operate3));
    }
    }
}
void minimum(string &s,int size)
{
	cout<<minimumhelp(s)<<endl;
	done.clear();
}
int main()
{
    int i,j;
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
   while(t--)
   {
   	 string s;
   	 cin>>s;
   	 int size=sizeof(s);
   	 minimum(s,size);
  	} 
	return 0;
}

Generated URL:

Output:

0
Report Bug