Help need to debug "Swap and Unite" Problem

Problem: Swap and Unite
Contest: Starters 124

I used the approach of prefix sum. But it gave wrong answer on the second test case.
What’s wrong with my code? Can somebody help me to debug this?

Code:

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

#define endl '\n'
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define ye "YES"
#define no "NO"
#define int long long

// Data Type
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int minOperation(string &str, char ch, int freq)

{
    int n = str.size();
    int preFreq[n];

    preFreq[0] = (str[0] == ch);
    for (int i = 1; i < n; i++)
        preFreq[i] = preFreq[i - 1] + (str[i] == ch);

    int mn = 1e8;
    for (int i = 0; i < n - freq; i++)
    {
        int curr = freq - (preFreq[i + freq - 1] - (i == 0 ? 0 : preFreq[i - 1]));
        mn = min(mn, curr);
    }

    return mn;
}

void solve()
{
    string str;
    cin >> str;

    vector<int> freq(26);
    for (int i = 0; i < str.size(); i++)
        freq[str[i] - 'a']++;

    int ans = 1e8;
    for (int i = 0; i < 26; i++)
    {
        if (freq[i])
            ans = min(ans, minOperation(str, 'a' + i, freq[i]));
    }

    cout << ans;
}

int32_t main()
{
    fastio;

    int t;
    cin >> t;

    while (t--)
    {
        solve();
        cout << '\n';
    }

    return 0;
}

@riyadhossain
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;
    }

}