Update the answer by subtracting the maxfrequency from total positions if there exist extra character at the end of the String apart from the n / k characters then add 1.
ans += ( totalpositions - maxfrequency + ( ( i % k < n % k ) ? 1 : 0 ) ) ; } document . write ( ans + " " ) ; }