All problems
0882EasyArrayHash TableStringBit ManipulationCounting

Labels the Press Can Set

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1684Count the Number of Consistent Strings

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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