Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
# [2185.Counting Words With a Given Prefix][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given an array of strings `words` and a string `pref`.

Return the number of strings in `words` that contain `pref` as a **prefix**.

A **prefix** of a string `s` is any leading contiguous substring of `s`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: words = ["pay","attention","practice","attend"], pref = "at"
Output: 2
Explanation: The 2 strings that contain "at" as a prefix are: "attention" and "attend".
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Counting Words With a Given Prefix
```go
```

Input: words = ["leetcode","win","loops","success"], pref = "code"
Output: 0
Explanation: There are no strings that contain "code" as a prefix.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
package Solution

func Solution(x bool) bool {
return x
type trieNode2185 struct {
c int
child [26]*trieNode2185
}

func insert(tree *trieNode2185, word string) {
cur := tree
for _, b := range word {
idx := b - 'a'
if cur.child[idx] == nil {
cur.child[idx] = &trieNode2185{}
}
cur.child[idx].c++
cur = cur.child[idx]
}
}

func search(tree *trieNode2185, target string) int {
cur := tree
for _, b := range target {
if cur.child[b-'a'] == nil {
return 0
}
cur = cur.child[b-'a']
}
return cur.c
}

func Solution(words []string, pref string) int {
tree := &trieNode2185{}
for _, w := range words {
insert(tree, w)
}
return search(tree, pref)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []string
prefix string
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []string{"pay", "attention", "practice", "attend"}, "at", 2},
{"TestCase2", []string{"leetcode", "win", "loops", "success"}, "code", 0},
}

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

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading