File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string[] } words
3+ * @param {string[] } puzzles
4+ * @return {number[] }
5+ */
6+ const findNumOfValidWords = function ( words , puzzles ) {
7+ let n = puzzles . length ,
8+ offset = 'a' . charCodeAt ( )
9+ let res = new Array ( n ) . fill ( 0 )
10+ let cnt = { }
11+
12+ for ( let w of words ) {
13+ let mask = 0
14+ for ( let c of w ) {
15+ mask |= 1 << ( c . charCodeAt ( ) - offset )
16+ }
17+ cnt [ mask ] = ~ ~ cnt [ mask ] + 1
18+ }
19+ for ( let i = 0 ; i < n ; i ++ ) {
20+ let s = puzzles [ i ] ,
21+ len = s . length
22+ for ( let k = 0 ; k < 1 << ( len - 1 ) ; k ++ ) {
23+ let mask = 1 << ( s [ 0 ] . charCodeAt ( ) - offset )
24+ for ( let j = 0 ; j < len - 1 ; j ++ ) {
25+ if ( k & ( 1 << j ) ) {
26+ mask |= 1 << ( s [ j + 1 ] . charCodeAt ( ) - offset )
27+ }
28+ }
29+ res [ i ] += ~ ~ cnt [ mask ]
30+ }
31+ }
32+ return res
33+ }
You can’t perform that action at this time.
0 commit comments