不难发现每个字符最多修改一次,那么我们统计第i%k位置的字符,全部修改为当前数量最多的字符就行了

#include <bits/stdc++.h>
#define il inline
#define double long double
using namespace std;
using ll = long long;
using ull = unsigned long long;
using int128 = __int128_t;

const ll N = 5e5 + 5, mod = 1e9+7, inf = 2e18;
const double eps = 1e-9;
double PI = 3.1415926;

il void solve(){
    int n,k;
    string s;
    cin>>n>>k>>s;
    vector<vector<int>>cnt(k,vector<int>(26));
    for(int i=0;i<n;i++){
        cnt[i%k][s[i]-'a']++;
    }
    int ans=0;
    for(int i=0;i<k;i++){
        int maxx=0,sum=0;
        for(int j=0;j<26;j++){
            maxx=max(maxx,cnt[i][j]);
            sum+=cnt[i][j];
        }
        ans+=(sum-maxx);
    }
    cout<<ans;
}

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int t = 1;

    // cin >> t;

    while (t--){

        solve();
    }

    return 0;
}