Skip to content

Commit 07b7c48

Browse files
authored
Update 792-number-of-matching-subsequences.js
1 parent d97b7a2 commit 07b7c48

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

792-number-of-matching-subsequences.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,34 @@ const numMatchingSubseq = function(S, words) {
5454
return res
5555
};
5656

57+
// another
58+
59+
/**
60+
* @param {string} s
61+
* @param {string[]} words
62+
* @return {number}
63+
*/
64+
const numMatchingSubseq = function(s, words) {
65+
const hash = {}
66+
for(const w of words) {
67+
const ch = w[0], it = w[Symbol.iterator]()
68+
if(hash[ch] == null) hash[ch] = []
69+
hash[ch].push(it)
70+
it.next()
71+
}
72+
let res = 0
73+
for(const e of s) {
74+
const arr = hash[e] || []
75+
hash[e] = []
76+
for(const it of arr) {
77+
const { value, done } = it.next()
78+
if(done) res++
79+
else {
80+
if(hash[value] == null) hash[value] = []
81+
hash[value].push(it)
82+
}
83+
}
84+
}
85+
86+
return res
87+
};

0 commit comments

Comments
 (0)