Count the Number of Consistent Strings
Real-world version: Labels the Press Can SetThe 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.
def count_consistent_strings(allowed: str, words: list[str]) -> int:public int countConsistentStrings(String allowed, String[] words)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Count Pairs Of Similar StringsReal-world version: Pairs of Labels Using the Same LettersEasy
- Maximum Score Words Formed by LettersReal-world version: Best Tile Rack PlayHard
- Find Words That Can Be Formed by CharactersReal-world version: Signs Spellable From A Tile TrayEasy
- Most Common WordReal-world version: The Commonest Allowed WordEasy