Help me in solving SWAPUNITE problem

My issue

help

My code

#include<bits/stdc++.h>
#define int long long int
using namespace std;
const int N=1e5+10;
int dp[N];
 
void solve(){
     
     string s;
     cin>>s;
     int n=(int)s.size();
     map<char,int> mp;
     for(auto &it:s){
        mp[it]++;
     }

     int ans=INT_MAX;
     for(char ch='a' ; ch<='z' ; ch++){
        int c=0;
        if(mp[ch]==0) continue;
        for(int i=0 ; i<n ; i++){
                if(s[i]==ch){
                    c++;
                    if(i==n-1) ans=min(mp[ch]-c,ans);
                }else{
                    ans=min(mp[ch]-c,ans);
                    c=0;
                }
        }
     }

     cout<<ans<<"\n";
}
 
int32_t main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
   
    int t=1;
    std::cin >> t;
    for(int i=0 ; i<t  ; i++){
        solve();
    }
   
    return 0;
}


/*
              If debugging is the process of removing software bugs,
              then programming must be the process of putting them in.
            - Edsger Dijkstra
*/

Problem Link: Swap and Unite Practice Coding Problem - CodeChef

You are not doing it correctly
you have to find the count of ch in subarray of size mp[ch]
Do this using sliding window
https://www.codechef.com/viewsolution/1049151383

@tanishk_07
here plzz refer my c++ code for better understanding

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        map<char,int> mp;
        for(int i=0;i<s.size();i++)
        {
            mp[s[i]]++;
        }
        int ans=INT_MAX;
        for(auto x:mp)
        {
         map<char,int> mp1;   
            for(int i=0;i<x.second;i++)
            {
                mp1[s[i]]++;
            }
           ans=min(ans,x.second-mp1[x.first]);
           for(int i=x.second;i<s.size();i++)
           {
               mp1[s[i]]++;
               mp1[s[i-x.second]]--;
               ans=min(ans,x.second-mp1[x.first]);
           }
        }
        cout<<ans<<endl;
    }

}