Interview Questions/Coding/Good Substring Catalog

Good Substring Catalog

Preview mode. Log in to edit, run, submit, and save progress.

Hard

Good Substring Catalog

A moderation service catalogs substrings from a lowercase message. The service receives a 26-character quality mask: the i-th character says whether the i-th lowercase letter is good or bad. A substring is accepted into the catalog only if it contains at most k bad letters. For each message, count how many distinct accepted substrings exist. Different occurrences of the same text count only once. For example, if the substring ab appears multiple times and is accepted, it contributes one catalog entry. Input Format: The first line contains t. Each case contains the message string s, then the 26-character quality mask, then the integer k. Output Format: For each case, print the number of distinct substrings that contain at most k bad letters.

Examples

Example 1
Input:
1
abab
11111111111111111111111111
1
Output:
7

Explanation: All letters are marked good in the sample, so every distinct substring of abab is accepted. The distinct entries are a, b, ab, ba, aba, bab, and abab.

Approach hint

Stop extending a substring once it has too many bad letters.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp