#include <bits/stdc++.h>
using namespace std;
string s;
vector<vector<int> > dp;
int n;
int rec(int i,int j){
if(i>=j)return 0;
if(dp[i][j]!=-1)return dp[i][j];
if(s[i]==s[j]){
return dp[i][j]=rec(i+1,j-1);
}
else return dp[i][j]=1+min(rec(i+1,j),rec(i,j-1));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
cin>>ws;
cin>>s;
n=s.size();
dp.assign(n+1,vector<int>(n+1,-1));
cout<<rec(0,n-1)<<'\n';
}
}
Comments
Post a Comment