Yet Another Broken Keyboard

You are given a string s consisting of lowercase English letters, and a character array keys representing the only working letters on a broken keyboard.

Your task is to find the total number of contiguous substrings of s that you can successfully type. A substring can be typed if and only if every single character within it is present in the keys array.

Calculate and return this total count.

Example 1:

Input: s = "abacaba", keys = ['a', 'b']

Output: 12

Explanation:

The valid continuous segments are "aba" and "aba". A segment of length L yields L * (L + 1) / 2 substrings. Each "aba" (length 3) yields 6 substrings. Total = 6 + 6 = 12.

Example 2:

Input: s = "sadfaasdda", keys = ['f', 'a', 'd']

Output: 21

Explanation:

The valid segments separated by broken keys are "adfaa" (length 5) and "dda" (length 3). Total substrings = 15 + 6 = 21.

Now Your Turn!

Pick the correct output for the given input

Input: s = "helloworld", keys = ['l', 'o']

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= s.length <= 2 * 105
  • 1 <= keys.length <= 26
  • s and keys consist of lowercase English letters.
  • All characters in keys are distinct.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
long long countSubstrings(string s, vector<char>& keys) {
}
};
Test Case

Input:

Keys
S