Question bank › Sliding Window
Dsa Sliding Window Medium

Consecutive K Distinct Characters

You are given a string `s` and an integer `k`. Your task is to find the number of substrings of `s` that contain at most `k` distinct characters. Implement a function that reads from standard input and prints the count to standard output. ### Input Format: - A single line string `s` (1 <= len(s) <= 10^5). - An integer `k` (0 <= k <= 26). ### Output Format: - An integer representing the number of substrings that contain at most `k` distinct characters. ### Example: Input: abcabc 2 Output: 7 In the example, the substrings with at most 2 distinct characters are ["ab", "ab", "bc", "ca", "abc", "a", "b", "c", "a", "b"] totaling to 7.

Key concepts

sliding_windowhashmapstrings

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of Praxari's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.