> For the complete documentation index, see [llms.txt](https://nataliekung.gitbook.io/ladder_code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nataliekung.gitbook.io/ladder_code/amz-oa/k-substring-with-k-different-characterssliding-window.md).

# K-Substring with K different characters(sliding window)

## Description

Given a string S and an integer K.\
Calculate the number of substrings of length K and containing K different characters

Have you met this question in a real interview?

Yes

Problem Correction

## Example

```
String: "abcabc"
K: 3

Answer: 3
substrings:  ["abc", "bca", "cab"]
```

```
String: "abacab"
K: 3

Answer: 2
substrings: ["bac", "cab"]
```

分析

for 里面I做end指针，直接用i-k，不用start指针了。

几大元素：start, end, map, count, size of substring(maybe)

这里count检测map进入1和退出0的情况，然后++ --

这里需要set 为了去重 比如重复出现的ab

```
class Solution:
    """
    @param stringIn: The original string.
    @param K: The length of substrings.
    @return: return the count of substring of length K and exactly K distinct characters.
    """
    import collections
    def KSubstring(self, stringIn, K):
        # Write your code here
        map = collections.defaultdict(int)
        i=j=0
        cnt = 0
        res = set()
        strLen = len(stringIn)
        for i in range(strLen):
            c = stringIn[i]
            map[c] += 1
            if map[c] == 1:
                cnt += 1
            if i >= K:
                c = stringIn[i - K]
                map[c] -= 1
                if map[c] == 0:
                    cnt -= 1
            if cnt == K:
                res.add(stringIn[i - K + 1:i + 1])
        return len(res)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://nataliekung.gitbook.io/ladder_code/amz-oa/k-substring-with-k-different-characterssliding-window.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
