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

Last updated

Was this helpful?