Map to store strings of length k and their counts
let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i += k ) { if ( mp . has ( str . substring ( i , i + k ) ) ) mp . set ( str . substring ( i , i + k ) , mp . get ( str . substring ( i , i + k ) ) + 1 ) ; else mp . set ( str . substring ( i , i + k ) , 1 ) ; }