#1178

Number of Valid Words for Each Puzzle

candidate master · 1490 · lc hard +32 · verified · 47.7% accepted · 1,301 likes · top 33%

Description

For a given puzzle string, a word is valid only when both conditions hold:

- word contains the first character of puzzle.

- Every character in word also appears somewhere in puzzle.

- For example, with puzzle "abcdefg", valid words include "faced", "cabbage", and "baggage", while "beefed" (missing 'a') and "based" (has 's' not in puzzle) are invalid.

Return an array answer where answer[i] is the count of words from words that are valid for puzzles[i].

Example 1:

Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"]
Output: [1,1,3,2,4,0]
Explanation:
1 valid word for "aboveyz" : "aaaa"
1 valid word for "abrodyz" : "aaaa"
3 valid words for "abslute" : "aaaa", "asas", "able"
2 valid words for "absoryz" : "aaaa", "asas"
4 valid words for "actresz" : "aaaa", "asas", "actt", "access"
There are no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.

Example 2:

Input: words = ["apple","pleas","please"], puzzles = ["aelwxyz","aelpxyz","aelpsxy","saelpxy","xaelpsy"]
Output: [0,1,3,2,0]

Code

1
2
3