|
1 |
| -import { counter } from "../substring-with-concatenation-of-all-words/counter.ts"; |
2 |
| - |
3 |
| -function numMatchingSubseq(s: string, words: string[]): number { |
4 |
| - const cnt = counter(words); |
5 |
| - const arr = Array(26) |
6 |
| - .fill(0) |
7 |
| - .map(() => new Set<[string, number]>()); |
8 |
| - |
9 |
| - for (const [w, count] of cnt) { |
10 |
| - arr[w.charCodeAt(0) - "a".charCodeAt(0)].add([w, count]); |
11 |
| - } |
12 |
| - let res = 0; |
13 |
| - for (const c of s) { |
14 |
| - const temp = arr[c.charCodeAt(0) - "a".charCodeAt(0)]; |
15 |
| - for (const wordcount of temp) { |
16 |
| - const [word, count1] = wordcount; |
17 |
| - if (word.length === 1) { |
18 |
| - res += count1; |
19 |
| - temp.delete(wordcount); |
20 |
| - } else { |
21 |
| - const w1 = word.slice(1); |
22 |
| - if (w1.charCodeAt(0) === word.charCodeAt(0)) { |
23 |
| - wordcount[0] = word.slice(1); |
24 |
| - } else { |
25 |
| - temp.delete(wordcount); |
26 |
| - arr[w1.charCodeAt(0) - "a".charCodeAt(0)].add([w1, count1]); |
27 |
| - } |
28 |
| - } |
29 |
| - } |
30 |
| - } |
31 |
| - |
32 |
| - return res; |
33 |
| -} |
34 |
| -export default numMatchingSubseq; |
| 1 | +import { counter } from "../substring-with-concatenation-of-all-words/counter.ts"; |
| 2 | + |
| 3 | +function numMatchingSubseq(s: string, words: string[]): number { |
| 4 | + const cnt = counter(words); |
| 5 | + const arr = Array(26) |
| 6 | + .fill(0) |
| 7 | + .map(() => new Set<[string, number]>()); |
| 8 | + |
| 9 | + for (const [w, count] of cnt) { |
| 10 | + arr[w.charCodeAt(0) - "a".charCodeAt(0)].add([w, count]); |
| 11 | + } |
| 12 | + let res = 0; |
| 13 | + for (const c of s) { |
| 14 | + const temp = arr[c.charCodeAt(0) - "a".charCodeAt(0)]; |
| 15 | + for (const wordcount of temp) { |
| 16 | + const [word, count1] = wordcount; |
| 17 | + if (word.length === 1) { |
| 18 | + res += count1; |
| 19 | + temp.delete(wordcount); |
| 20 | + } else { |
| 21 | + const w1 = word.slice(1); |
| 22 | + if (w1.charCodeAt(0) === word.charCodeAt(0)) { |
| 23 | + wordcount[0] = word.slice(1); |
| 24 | + } else { |
| 25 | + temp.delete(wordcount); |
| 26 | + arr[w1.charCodeAt(0) - "a".charCodeAt(0)].add([w1, count1]); |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + return res; |
| 33 | +} |
| 34 | +export default numMatchingSubseq; |
0 commit comments