Count Strings With Required Characters - Problem
You are given a string required containing distinct lowercase English letters and an array of strings words.
A word is considered matching if it contains at least one character that also appears in required.
Return the count of matching words in the array words.
Input & Output
Example 1 — Some Match
$
Input:
required = "ae", words = ["apple","box","cat","dog","egg"]
›
Output:
3
💡 Note:
"apple" contains 'a' and 'e', "cat" contains 'a', "egg" contains 'e'. "box" and "dog" contain neither 'a' nor 'e'.
Example 2 — All Match
$
Input:
required = "xyz", words = ["fox","yell","buzz","ax","lazy"]
›
Output:
5
💡 Note:
Every word contains at least one of 'x', 'y', or 'z': fox(x), yell(y), buzz(z), ax(x), lazy(y,z).
Example 3 — None Match
$
Input:
required = "q", words = ["hello","world","abc","def"]
›
Output:
0
💡 Note:
No word contains the character 'q'.
Constraints
- 1 ≤ words.length ≤ 10⁴
- 1 ≤ required.length ≤ 26
- 1 ≤ words[i].length ≤ 10
- The characters in required are distinct
- words[i] and required contain only lowercase English letters
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code