Skip to content

Commit 04be4ca

Browse files
committed
prefix-and-suffix-search
1 parent 128a6e0 commit 04be4ca

File tree

3 files changed

+57
-27
lines changed

3 files changed

+57
-27
lines changed

prefix-and-suffix-search/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/masx200/leetcode-test/prefix-and-suffix-search
2+
3+
go 1.19

prefix-and-suffix-search/index.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package index
2+
3+
type WordFilter struct {
4+
d map[string]int
5+
}
6+
7+
func Constructor(words []string) WordFilter {
8+
mp := map[string]int{}
9+
for i, word := range words {
10+
n := len(word)
11+
for j := 1; j <= n; j++ {
12+
for k := 1; k <= n; k++ {
13+
mp[word[:j]+","+word[n-k:]] = i
14+
}
15+
}
16+
}
17+
return WordFilter{d: mp}
18+
}
19+
20+
func (w *WordFilter) F(pref string, suff string) int {
21+
v, ok := w.d[pref+","+suff]
22+
if ok {
23+
return v
24+
} else {
25+
return -1
26+
}
27+
}

prefix-and-suffix-search/index.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +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;
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)