-
Notifications
You must be signed in to change notification settings - Fork 64
/
统计追加字母可以获得的单词数.js
44 lines (43 loc) · 1.09 KB
/
统计追加字母可以获得的单词数.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* @param {string[]} startWords
* @param {string[]} targetWords
* @return {number}
2135. 统计追加字母可以获得的单词数
'a'.charCodeAt() // 97
'z'.charCodeAt() // 122
m startWords.length
n targetWords.length
k
nmk的平方
startWords = ["ant","act","tack"], targetWords = ["tack","act","acti"]
*/
var wordCount = function(startWords, targetWords) {
let ans = 0;
const set = new Set();
const pos = x => x.charCodeAt() - 'a'.charCodeAt();
for (const word of startWords) {
let x = 0;
for (const char of word) {
const offset = pos(char);
x = x | (1 << offset)
}
set.add(x);
}
for (const target of targetWords) {
let x = 0;
for (const char of target) {
const offset = pos(char);
x = x | (1 << offset)
}
for (const char of target) {
// t a c k = x
const offset = pos(char);
const t = x ^ (1 << offset);
if (set.has(t)) {
ans++;
break;
}
}
}
return ans;
};