#
# 
# @param s string字符串 s.size() <= 1e5
# @param k int整型 k <= s.size()
# @return int整型
#
class Solution:
    def turn(self , s , k ):
        # write code here
        # 每个子串里,每个字母前面有几个比自己大的,就交换几次 
        # 例:‘abc’ 交换0+1+2=3次
        steps = 0
        for i in range(k):
            num = [0] * 26
            for j in range(i, len(s), k):
                index = ord(s[j]) - ord('a')
                num[index] += 1
                for m in range(index):
                    steps += num[m]
        return steps

        # 超时
        # import math
        # res = 0
        # s_list = list(s)
        # for j in range(math.floor(len(s_list)/k)):
        #     for i in range(len(s_list)-k):
        #         if s_list[i] < s_list[i+k]:
        #             s_list[i], s_list[i+k] = s_list[i+k], s_list[i]
        #             res += 1

        # return res