File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ leetcode 测试
10
10
11
11
##### 包含的内容如下
12
12
13
+ https://leetcode.cn/problems/prefix-and-suffix-search/
14
+
13
15
https://leetcode.cn/problems/reverse-prefix-of-word/
14
16
15
17
https://leetcode.cn/problems/counting-words-with-a-given-prefix/
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments