Map to store strings of length k and their counts
Dictionary < String , int > mp = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < n ; i += k ) { if ( ! mp . ContainsKey ( str . Substring ( i , k ) ) ) mp . Add ( str . Substring ( i , k ) , 1 ) ; else mp [ str . Substring ( i , k ) ] = mp [ str . Substring ( i , k ) ] + 1 ; }