Skip to content

Commit 128a6e0

Browse files
committed
https://leetcode.cn/problems/prefix-and-suffix-search/
1 parent d6f770c commit 128a6e0

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/prefix-and-suffix-search/
14+
1315
https://leetcode.cn/problems/reverse-prefix-of-word/
1416

1517
https://leetcode.cn/problems/counting-words-with-a-given-prefix/

prefix-and-suffix-search/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class WordFilter {
2+
#d = new Map<string, number>();
3+
4+
constructor(words: string[]) {
5+
for (const [k, word] of words.entries()) {
6+
// this.#d.set(hash(word, word), k);
7+
for (let i = 1; i <= word.length; i++) {
8+
for (let j = 1; j <= word.length; j++) {
9+
const pref = word.substring(0, i);
10+
const suff = word.substring(word.length - j);
11+
// console.log(pref,suff,word,k)
12+
this.#d.set(hash(pref, suff), k);
13+
}
14+
}
15+
}
16+
// console.log(this.#d);
17+
}
18+
19+
f(pref: string, suff: string): number {
20+
return this.#d.get(hash(pref, suff)) ?? -1;
21+
}
22+
}
23+
24+
function hash(pref: string, suff: string) {
25+
return pref + "," + suff;
26+
}
27+
export default WordFilter;

0 commit comments

Comments
 (0)