Show problems as
Classic problem #1684EasyArrayHash TableStringBit ManipulationCounting

Count the Number of Consistent Strings

Real-world version: Labels the Press Can Set
Tracked in this browser only
Read Count the Number of Consistent Strings on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A hand press holds type for the distinct letters listed in allowed, and it can set a label only if every letter of that label is among them. A letter may be used as often as needed.

Given the labels in words, return how many of them the press can set.

Examples

Example 1

Input
allowed = "fedcba", words = ["cafe", "faced", "deface", "bagged", "zebra"]
Output
3

The press holds the first six letters. "cafe", "faced" and "deface" use nothing else, while "bagged" needs a g and "zebra" needs a z.

Example 2

Input
allowed = "xy", words = ["z", "xx", "yyy", "xyxy"]
Output
3

A letter may be repeated as often as needed, so the last three all work. Only "z" is out of reach.

Example 3

Input
allowed = "a", words = ["b", "c", "d"]
Output
0

None of the labels use the single letter on hand.

Constraints

  • 1 <= words.length <= 10^4
  • 1 <= allowed.length <= 26
  • 1 <= words[i].length <= 10
  • The letters in allowed are all different
  • allowed and every entry of words consist of lowercase English letters only

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def count_consistent_strings(allowed: str, words: list[str]) -> int:
Java
public int countConsistentStrings(String allowed, String[] words)