Count number of substrings
Easy Accuracy: 38.29% Submissions: 4138 Points: 2

Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters.

Input:

The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of a string S.
The second line of each test case contains integer K.

Output:

Corresponding to each test case, in a new line, print the count all possible substrings that has exactly k distinct characters.

Constraints:

1 ≤ T ≤ 100
1 ≤ string length ≤ 1000
1 ≤ K ≤ 26

Example:

Input
1
aba
2

Output
3

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.