Logical error in the SWAPUNITE code

#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;

void solve()
{
    string s;
    cin >> s;
    
    int n = s.size();
    int hash[26] = {0};
    
    for(int i = 0; i < n; ++i)
    {
        hash[s[i] - 'a']++;
    }
    
    int cnt = 1;
    int mini = 1e9;
    for(int i = 1; i < n; ++i)
    {
        if(s[i] == s[i-1])
        {
            cnt++;
        }
        else
        {
            mini = min(mini, hash[s[i-1] - 'a'] - cnt);
            cnt = 1;
        }
    }
    
    mini = min(mini, hash[s.back() - 'a'] - cnt);
    
    cout << mini << '\n';
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int t;
    cin >> t;
    
    while(t--)
        solve();
}

whats wrong in this approach ?? I counted the frequencies and now i count the consecutive frequencies of the characters and then compare them with the total frequency of that character to know how many swaps will it need to get all together and have maintained the mini variable that tracks the mini swaps

Consider the test case:

1
abbabbaba

Only one swap is sufficient to form a contiguous segment of all b’s.
When considering a contiguous segment of character c in s, some occurrences of c might already be in-place when forming the segment of all c's.

Thanks, I got it, should’ve used sliding window instead of frequency counts.