Skip to content

Commit c45d988

Browse files
authored
Merge pull request #1088 from 0xff-dev/2185
Add solution and test-cases for problem 2185
2 parents 00b2017 + 2a33a5a commit c45d988

File tree

3 files changed

+55
-24
lines changed

3 files changed

+55
-24
lines changed

leetcode/2101-2200/2185.Counting-Words-With-a-Given-Prefix/README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
# [2185.Counting Words With a Given Prefix][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an array of strings `words` and a string `pref`.
5+
6+
Return the number of strings in `words` that contain `pref` as a **prefix**.
7+
8+
A **prefix** of a string `s` is any leading contiguous substring of `s`.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: words = ["pay","attention","practice","attend"], pref = "at"
14+
Output: 2
15+
Explanation: The 2 strings that contain "at" as a prefix are: "attention" and "attend".
1316
```
1417

15-
## 题意
16-
> ...
18+
**Example 2:**
1719

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Counting Words With a Given Prefix
23-
```go
2420
```
25-
21+
Input: words = ["leetcode","win","loops","success"], pref = "code"
22+
Output: 0
23+
Explanation: There are no strings that contain "code" as a prefix.
24+
```
2625

2726
## 结语
2827

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type trieNode2185 struct {
4+
c int
5+
child [26]*trieNode2185
6+
}
7+
8+
func insert(tree *trieNode2185, word string) {
9+
cur := tree
10+
for _, b := range word {
11+
idx := b - 'a'
12+
if cur.child[idx] == nil {
13+
cur.child[idx] = &trieNode2185{}
14+
}
15+
cur.child[idx].c++
16+
cur = cur.child[idx]
17+
}
18+
}
19+
20+
func search(tree *trieNode2185, target string) int {
21+
cur := tree
22+
for _, b := range target {
23+
if cur.child[b-'a'] == nil {
24+
return 0
25+
}
26+
cur = cur.child[b-'a']
27+
}
28+
return cur.c
29+
}
30+
31+
func Solution(words []string, pref string) int {
32+
tree := &trieNode2185{}
33+
for _, w := range words {
34+
insert(tree, w)
35+
}
36+
return search(tree, pref)
537
}

leetcode/2101-2200/2185.Counting-Words-With-a-Given-Prefix/Solution_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []string
14+
prefix string
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []string{"pay", "attention", "practice", "attend"}, "at", 2},
18+
{"TestCase2", []string{"leetcode", "win", "loops", "success"}, "code", 0},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.prefix)
2525
if !reflect.DeepEqual(got, c.expect) {
2626
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2727
c.expect, got, c.inputs)
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)