Iterate over the string with increment of K
for ( let j = i ; j < N ; j += K ) { if ( M . has ( S [ j ] ) ) { M . set ( M . get ( S [ j ] ) , M . get ( S [ j ] ) + 1 ) ; } else { M . set ( S [ j ] , 1 ) ; } }