diff --git a/leetcode/1001-1100/1032.Stream-of-Characters/README.md b/leetcode/1001-1100/1032.Stream-of-Characters/README.md index a03ca6380..f725d2ede 100644 --- a/leetcode/1001-1100/1032.Stream-of-Characters/README.md +++ b/leetcode/1001-1100/1032.Stream-of-Characters/README.md @@ -1,28 +1,39 @@ # [1032.Stream of Characters][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 +Design an algorithm that accepts a stream of characters and checks if a suffix of these characters is a string of a given array of strings `words`. -**Example 1:** +For example, if `words = ["abc", "xyz"]` and the stream added the four characters (one by one) `'a'`, `'x'`, `'y'`, and `'z'`, your algorithm should detect that the suffix `"xyz"` of the characters `"axyz"` matches `"xyz"` from `words`. -``` -Input: a = "11", b = "1" -Output: "100" -``` +Implement the `StreamChecker` class: -## 题意 -> ... +- `StreamChecker(String[] words)` Initializes the object with the strings array `words`. +- `boolean query(char letter)` Accepts a new character from the stream and returns `true` if any non-empty suffix from the stream forms a word that is in `words`. -## 题解 +**Example 1:** -### 思路1 -> ... -Stream of Characters -```go ``` - +Input +["StreamChecker", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query"] +[[["cd", "f", "kl"]], ["a"], ["b"], ["c"], ["d"], ["e"], ["f"], ["g"], ["h"], ["i"], ["j"], ["k"], ["l"]] +Output +[null, false, false, false, true, false, true, false, false, false, false, false, true] + +Explanation +StreamChecker streamChecker = new StreamChecker(["cd", "f", "kl"]); +streamChecker.query("a"); // return False +streamChecker.query("b"); // return False +streamChecker.query("c"); // return False +streamChecker.query("d"); // return True, because 'cd' is in the wordlist +streamChecker.query("e"); // return False +streamChecker.query("f"); // return True, because 'f' is in the wordlist +streamChecker.query("g"); // return False +streamChecker.query("h"); // return False +streamChecker.query("i"); // return False +streamChecker.query("j"); // return False +streamChecker.query("k"); // return False +streamChecker.query("l"); // return True, because 'kl' is in the wordlist +``` ## 结语 diff --git a/leetcode/1001-1100/1032.Stream-of-Characters/Solution.go b/leetcode/1001-1100/1032.Stream-of-Characters/Solution.go index d115ccf5e..9e7bc24e3 100644 --- a/leetcode/1001-1100/1032.Stream-of-Characters/Solution.go +++ b/leetcode/1001-1100/1032.Stream-of-Characters/Solution.go @@ -1,5 +1,75 @@ package Solution -func Solution(x bool) bool { - return x +type trie1032 struct { + end bool + child [26]*trie1032 +} + +type StreamChecker struct { + // trie + i int + ml int + q []byte // max length + t *trie1032 +} + +func Constructor1032(words []string) StreamChecker { + s := StreamChecker{ + t: &trie1032{}, + } + ml := 0 + for _, w := range words { + ml = max(ml, len(w)) + s.fill(w) + } + + s.q = make([]byte, ml) + s.ml = ml + return s +} + +func (this *StreamChecker) fill(word string) { + root := this.t + for i := len(word) - 1; i >= 0; i-- { + b := word[i] + if root.child[b-'a'] == nil { + root.child[b-'a'] = &trie1032{} + } + if i == 0 { + root.child[b-'a'].end = true + } + root = root.child[b-'a'] + } +} + +func (this *StreamChecker) Query(letter byte) bool { + if this.i == this.ml { + next := make([]byte, this.ml) + copy(next, this.q[1:]) + this.i = this.ml - 1 + this.q = next + } + this.q[this.i] = letter + this.i++ + root := this.t + for i := this.i - 1; i >= 0; i-- { + cur := this.q[i] - 'a' + if root.child[cur] == nil { + return false + } + if root.child[cur].end { + return true + } + root = root.child[cur] + } + return false +} + +func Solution(words []string, query []byte) []bool { + ans := make([]bool, len(query)) + c := Constructor1032(words) + for i := range query { + ans[i] = c.Query(query[i]) + } + return ans } diff --git a/leetcode/1001-1100/1032.Stream-of-Characters/Solution_test.go b/leetcode/1001-1100/1032.Stream-of-Characters/Solution_test.go index 14ff50eb4..5d5e8ae97 100644 --- a/leetcode/1001-1100/1032.Stream-of-Characters/Solution_test.go +++ b/leetcode/1001-1100/1032.Stream-of-Characters/Solution_test.go @@ -10,30 +10,29 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + words []string + query []byte + expect []bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []string{"cd", "f", "kl"}, []byte("abcdefghijkl"), []bool{false, false, false, true, false, true, false, false, false, false, false, true}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.words, c.query) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.words, c.query) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }