1178. Number of Valid Words for Each Puzzle Hard

@problem@discussion
#Array#Hash Table#String#Bit Manipulation#Trie



1/**
2 * [1178] Number of Valid Words for Each Puzzle
3 *
4 * With respect to a given puzzle string, a word is valid if both the following conditions are satisfied:
5 * 	word contains the first letter of puzzle.
6 * 	For each letter in word, that letter is in puzzle.
7 * 	
8 * 		For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage", while
9 * 		invalid words are "beefed" (does not include 'a') and "based" (includes 's' which is not in the puzzle).
10 * 	
11 * 	
12 * Return an array answer, where answer[i] is the number of words in the given word list words that is valid with respect to the puzzle puzzles[i].
13 *  
14 * Example 1:
15 * 
16 * Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"]
17 * Output: [1,1,3,2,4,0]
18 * Explanation: 
19 * 1 valid word for "aboveyz" : "aaaa" 
20 * 1 valid word for "abrodyz" : "aaaa"
21 * 3 valid words for "abslute" : "aaaa", "asas", "able"
22 * 2 valid words for "absoryz" : "aaaa", "asas"
23 * 4 valid words for "actresz" : "aaaa", "asas", "actt", "access"
24 * There are no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.
25 * 
26 * Example 2:
27 * 
28 * Input: words = ["apple","pleas","please"], puzzles = ["aelwxyz","aelpxyz","aelpsxy","saelpxy","xaelpsy"]
29 * Output: [0,1,3,2,0]
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	1 <= words.length <= 10^5
35 * 	4 <= words[i].length <= 50
36 * 	1 <= puzzles.length <= 10^4
37 * 	puzzles[i].length == 7
38 * 	words[i] and puzzles[i] consist of lowercase English letters.
39 * 	Each puzzles[i] does not contain repeated characters.
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/number-of-valid-words-for-each-puzzle/
45// discuss: https://leetcode.com/problems/number-of-valid-words-for-each-puzzle/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn find_num_of_valid_words(words: Vec<String>, puzzles: Vec<String>) -> Vec<i32> {
51        vec![]
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_1178() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.