-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubstrings.py
More file actions
36 lines (30 loc) · 740 Bytes
/
Substrings.py
File metadata and controls
36 lines (30 loc) · 740 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Substrings with K distinct letters
# Given string str of lowercase alphabets of length n and an integer K, the task is to count all substrings of length K which have exactly K distinct characters.
l=list(map(int,input().split()))
v=input()
start=0
end=0
k=l[1]
words={v[0]:1}
count=1
dis=0
while(start<len(v)):
if(end-start+1<k):
end=end+1
if(end>=len(v)):
break
if v[end] not in words:
words[v[end]]=1
count=count+1
else:
words[v[end]]=words[v[end]]+1
else:
words[v[start]]=words[v[start]]-1
if words[v[start]]==0:
words.pop(v[start],None)
count=count-1
start=start+1
if(count==k and end-start+1==k):
dis=dis+1
# print(v[start:end+1])
print(dis)