Close
Topic list
Count with K different characters
EASY
15 mins
Strings
Topics (Covered in this problem)
Problem solved
Skill meter
Strings
-
-
Other topics
Problem solved
Skill meter
Matrices (2D Arrays)
-
-
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Count with K different characters

Contributed by
Dhruv Sharma
Easy
0/40
Avg time to solve 15 mins
Success Rate 85 %
Share

## Problem Statement

#### For example:

``````'STR' = “abcad” and K = 2.
We can see that the substrings {ab, bc, ca, ad} are the only substrings with 2 distinct characters. Therefore, the answer will be 4.
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images )

#### Sample Input 1 :

``````2
cdc
2
aacfssa
3
``````

#### Sample Output 1 :

``````3
5
``````

#### Explanation of The Sample Input 1:

``````For the first test case:
Given STR = "cdc”, we can see that the substrings with only 2 distinct characters are {cd, dc, cdc}. Therefore, the answer will be 3.

For the second test case:
Given STR = “aacfssa”. We can see that the substrings with only 3 distinct characters are {acf, cfs, aacf, cfss, fssa}. Therefore, the answer will be 5.
``````

#### Sample Input 2 :

``````3
aakash
2
bbbb
1
qffds
5
``````

#### Sample Output 2 :

``````7
10
0
``````
Auto
Console