From 2a33a5aff86916499eab9954bad13a5f90b8efb0 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 9 Jan 2025 08:57:56 +0800 Subject: [PATCH] Add solution and test-cases for problem 2185 --- .../README.md | 27 +++++++------- .../Solution.go | 36 +++++++++++++++++-- .../Solution_test.go | 16 ++++----- 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/leetcode/2101-2200/2185.Counting-Words-With-a-Given-Prefix/README.md b/leetcode/2101-2200/2185.Counting-Words-With-a-Given-Prefix/README.md index 60b610eec..05969b5a0 100755 --- a/leetcode/2101-2200/2185.Counting-Words-With-a-Given-Prefix/README.md +++ b/leetcode/2101-2200/2185.Counting-Words-With-a-Given-Prefix/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/2101-2200/2185.Counting-Words-With-a-Given-Prefix/Solution.go b/leetcode/2101-2200/2185.Counting-Words-With-a-Given-Prefix/Solution.go index d115ccf5e..670b607a7 100644 --- a/leetcode/2101-2200/2185.Counting-Words-With-a-Given-Prefix/Solution.go +++ b/leetcode/2101-2200/2185.Counting-Words-With-a-Given-Prefix/Solution.go @@ -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) } diff --git a/leetcode/2101-2200/2185.Counting-Words-With-a-Given-Prefix/Solution_test.go b/leetcode/2101-2200/2185.Counting-Words-With-a-Given-Prefix/Solution_test.go index 14ff50eb4..c81bd140d 100644 --- a/leetcode/2101-2200/2185.Counting-Words-With-a-Given-Prefix/Solution_test.go +++ b/leetcode/2101-2200/2185.Counting-Words-With-a-Given-Prefix/Solution_test.go @@ -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) @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }