Skip to content

Commit ee74123

Browse files
add leetcode 1178 solution
1 parent f0c5270 commit ee74123

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package leetcode
2+
3+
/*
4+
匹配跟单词中的字母顺序,字母个数都无关,可以用bitmap压缩
5+
1. 记录word中 利用map记录各种bit标示的个数
6+
2. puzzles 中各个字母都不相同! 记录bitmap,然后搜索子空间中各种bit标识的个数的和
7+
因为puzzles长度最长是7,所以搜索空间 2^7
8+
*/
9+
func findNumOfValidWords(words []string, puzzles []string) []int {
10+
11+
wordBitStatusMap := make(map[uint32]int, 0)
12+
for _, w := range words {
13+
wordBitStatusMap[toBitMap([]byte(w))]++
14+
}
15+
var res []int
16+
for _, p := range puzzles {
17+
var bitMap uint32
18+
var totalNum int
19+
bitMap |= (1 << (p[0] - 'a')) //work中要包含 p 的第一个字母 所以这个bit位上必须是1
20+
findNum([]byte(p)[1:], bitMap, &totalNum, wordBitStatusMap)
21+
res = append(res, totalNum)
22+
}
23+
24+
return res
25+
}
26+
27+
func toBitMap(word []byte) uint32 {
28+
var res uint32
29+
for _, b := range word {
30+
res |= (1 << (b - 'a'))
31+
}
32+
return res
33+
}
34+
35+
//利用dfs 搜索 pussles的子空间
36+
func findNum(puzzles []byte, bitMap uint32, totalNum *int, m map[uint32]int) {
37+
if len(puzzles) == 0 {
38+
*totalNum = *totalNum + m[bitMap]
39+
return
40+
}
41+
42+
//不包含puzzles[0],即puzzles[0]对应bit是0
43+
findNum(puzzles[1:], bitMap, totalNum, m)
44+
45+
//包含puzzles[0],即puzzles[0]对应bit是1
46+
bitMap |= (1 << (puzzles[0] - 'a'))
47+
findNum(puzzles[1:], bitMap, totalNum, m)
48+
bitMap ^= (1 << (puzzles[0] - 'a')) //异或 清零
49+
50+
return
51+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package leetcode
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_findNumOfValidWords(t *testing.T) {
9+
10+
words1 := []string{"aaaa", "asas", "able", "ability", "actt", "actor", "access"}
11+
puzzles1 := []string{"aboveyz", "abrodyz", "abslute", "absoryz", "actresz", "gaswxyz"}
12+
13+
type args struct {
14+
words []string
15+
puzzles []string
16+
}
17+
tests := []struct {
18+
name string
19+
args args
20+
want []int
21+
}{
22+
// TODO: Add test cases.
23+
{"1", args{words: words1, puzzles: puzzles1}, []int{1, 1, 3, 2, 4, 0}},
24+
}
25+
for _, tt := range tests {
26+
t.Run(tt.name, func(t *testing.T) {
27+
if got := findNumOfValidWords(tt.args.words, tt.args.puzzles); !reflect.DeepEqual(got, tt.want) {
28+
t.Errorf("findNumOfValidWords() = %v, want %v", got, tt.want)
29+
}
30+
})
31+
}
32+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
3+
# [1178. Number of Valid Words for Each Puzzle](https://leetcode-cn.com/problems/number-of-valid-words-for-each-puzzle/)
4+
5+
6+
## 题目
7+
8+
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied:
9+
word contains the first letter of puzzle.
10+
For each letter in word, that letter is in puzzle.
11+
For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle).
12+
Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].
13+
14+
**Example :**
15+
16+
Input:
17+
words = ["aaaa","asas","able","ability","actt","actor","access"],
18+
puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"]
19+
Output: [1,1,3,2,4,0]
20+
Explanation:
21+
1 valid word for "aboveyz" : "aaaa"
22+
1 valid word for "abrodyz" : "aaaa"
23+
3 valid words for "abslute" : "aaaa", "asas", "able"
24+
2 valid words for "absoryz" : "aaaa", "asas"
25+
4 valid words for "actresz" : "aaaa", "asas", "actt", "access"
26+
There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.
27+
28+
**Constraints**:
29+
30+
1 <= words.length <= 10^5
31+
4 <= words[i].length <= 50
32+
1 <= puzzles.length <= 10^4
33+
puzzles[i].length == 7
34+
words[i][j], puzzles[i][j] are English lowercase letters.
35+
Each puzzles[i] doesn't contain repeated characters.
36+
37+
38+
## 题目大意
39+
40+
外国友人仿照中国字谜设计了一个英文版猜字谜小游戏,请你来猜猜看吧。
41+
42+
字谜的迷面 puzzle 按字符串形式给出,如果一个单词 word 符合下面两个条件,那么它就可以算作谜底:
43+
44+
单词 word 中包含谜面 puzzle 的第一个字母。
45+
单词 word 中的每一个字母都可以在谜面 puzzle 中找到。
46+
例如,如果字谜的谜面是 "abcdefg",那么可以作为谜底的单词有 "faced", "cabbage", 和 "baggage";而 "beefed"(不含字母 "a")以及 "based"(其中的 "s" 没有出现在谜面中)都不能作为谜底。
47+
返回一个答案数组 answer,数组中的每个元素 answer[i] 是在给出的单词列表 words 中可以作为字谜迷面 puzzles[i] 所对应的谜底的单词数目。
48+
49+
50+
51+
**示例:**
52+
53+
输入:
54+
words = ["aaaa","asas","able","ability","actt","actor","access"],
55+
puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"]
56+
输出:[1,1,3,2,4,0]
57+
**解释:**
58+
1 个单词可以作为 "aboveyz" 的谜底 : "aaaa"
59+
1 个单词可以作为 "abrodyz" 的谜底 : "aaaa"
60+
3 个单词可以作为 "abslute" 的谜底 : "aaaa", "asas", "able"
61+
2 个单词可以作为 "absoryz" 的谜底 : "aaaa", "asas"
62+
4 个单词可以作为 "actresz" 的谜底 : "aaaa", "asas", "actt", "access"
63+
没有单词可以作为 "gaswxyz" 的谜底,因为列表中的单词都不含字母 'g'。
64+
65+
**提示:**
66+
67+
1 <= words.length <= 10^5
68+
4 <= words[i].length <= 50
69+
1 <= puzzles.length <= 10^4
70+
puzzles[i].length == 7
71+
words[i][j], puzzles[i][j] 都是小写英文字母。
72+
每个 puzzles[i] 所包含的字符都不重复。
73+
74+
## 解题思路
75+
76+
首先题目中两个限制条件非常关键:
77+
78+
- puzzles[i].length == 7
79+
- 每个 puzzles[i] 所包含的字符都不重复
80+
81+
也就是说穷举每个puzzle的子串的搜索空间就是2^7=128,而且不用考虑去重问题。
82+
83+
1. 因为谜底的判断只跟字符是否出现有关,跟字符的个数无关,另外都是小写的英文字母,所以可以用`bitmap`来表示单词(word)。
84+
2. 利用`map`记录不同状态的单词(word)的个数。
85+
3. 根据题意,如果某个单词(word)是某个字谜(puzzle)的谜底,那么word的bitmap肯定对应于puzzle某个子串的bitmap表示,且bitmap中包含puzzle的第一个字母的bit占用。
86+
4. 问题就转换为:求每一个puzzle的每一个子串,然后求和这个子串具有相同bitmap表示且word中包含puzzle的第一个字母的word的个数。

0 commit comments

Comments
 (0)