diff --git a/leetcode/801-900/0809.Expressive-Words/README.md b/leetcode/801-900/0809.Expressive-Words/README.md index cb6c800eb..fcde1ab3a 100644 --- a/leetcode/801-900/0809.Expressive-Words/README.md +++ b/leetcode/801-900/0809.Expressive-Words/README.md @@ -1,28 +1,35 @@ # [809.Expressive Words][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 +Sometimes people repeat letters to represent extra feeling. For example: + +- `"hello" -> "heeellooo"` +- `"hi" -> "hiiii"` + +In these strings like `"heeellooo"`, we have groups of adjacent letters that are all the same: `"h"`, `"eee"`, `"ll"`, `"ooo"`. + +You are given a string `s` and an array of query strings `words`. A query word is **strectchy** if it can be made to be equal to `s` by any number of applications of the following extension operation: choose a group consisting of characters `c`, and add some number of characters `c` to the group so that the size of the group is **three or more**. + +- For example, starting with `"hello"`, we could do an extension on the group `"o"` to get `"hellooo"`, but we cannot get `"helloo"` since the group `"oo"` has a size less than three. Also, we could do another extension like `"ll" -> "lllll"` to get `"helllllooo"`. If `s = "helllllooo"`, then the query word `"hello"` would be **strectchy** because of these two extension operations: `query = "hello" -> "hellooo" -> "helllllooo" = s`. + +Return the number of query strings that are **strectchy**. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "heeellooo", words = ["hello", "hi", "helo"] +Output: 1 +Explanation: +We can extend "e" and "o" in the word "hello" to get "heeellooo". +We can't extend "helo" to get "heeellooo" because the group "ll" is not size 3 or more. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Expressive Words -```go ``` - +Input: s = "zzzzzyyyyy", words = ["zzyy","zy","zyy"] +Output: 3 +``` ## 结语 diff --git a/leetcode/801-900/0809.Expressive-Words/Solution.go b/leetcode/801-900/0809.Expressive-Words/Solution.go index d115ccf5e..9c137c624 100644 --- a/leetcode/801-900/0809.Expressive-Words/Solution.go +++ b/leetcode/801-900/0809.Expressive-Words/Solution.go @@ -1,5 +1,53 @@ package Solution -func Solution(x bool) bool { - return x +type char809 struct { + c byte + cc int +} + +func toChars(s string) []char809 { + pre := s[0] + c := 1 + chars := make([]char809, 0) + for i := 1; i < len(s); i++ { + b := s[i] + if b == pre { + c++ + continue + } + chars = append(chars, char809{c: byte(pre), cc: c}) + pre = b + c = 1 + } + chars = append(chars, char809{c: byte(pre), cc: c}) + return chars +} +func Solution(s string, words []string) int { + ans := 0 + target := toChars(s) + for _, word := range words { + tmp := toChars(word) + if len(tmp) != len(target) { + continue + } + ok := true + for i := range len(target) { + if target[i].c != tmp[i].c { + ok = false + break + } + if target[i].cc < 3 && tmp[i].cc != target[i].cc { + ok = false + break + } + if tmp[i].cc > target[i].cc { + ok = false + break + } + } + if ok { + ans++ + } + } + return ans } diff --git a/leetcode/801-900/0809.Expressive-Words/Solution_test.go b/leetcode/801-900/0809.Expressive-Words/Solution_test.go index 14ff50eb4..de51cc3a7 100644 --- a/leetcode/801-900/0809.Expressive-Words/Solution_test.go +++ b/leetcode/801-900/0809.Expressive-Words/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + s string + words []string + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "heeellooo", []string{"hello", "hi", "helo"}, 1}, + {"TestCase2", "zzzzzyyyyy", []string{"zzyy", "zy", "zyy"}, 3}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.s, c.words) 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.s, c.words) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }