diff --git a/leetcode/3301-3400/3333.Find-the-Original-Typed-String-II/README.md b/leetcode/3301-3400/3333.Find-the-Original-Typed-String-II/README.md index bf77b439..bb4c72aa 100755 --- a/leetcode/3301-3400/3333.Find-the-Original-Typed-String-II/README.md +++ b/leetcode/3301-3400/3333.Find-the-Original-Typed-String-II/README.md @@ -1,28 +1,45 @@ # [3333.Find the Original Typed String II][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 +Alice is attempting to type a specific string on her computer. However, she tends to be clumsy and **may** press a key for too long, resulting in a character being typed **multiple** times. + +You are given a string `word`, which represents the **final** output displayed on Alice's screen. You are also given a **positive** integer `k`. + +Return the total number of possible original strings that Alice might have intended to type, if she was trying to type a string of size **at least** `k`. + +Since the answer may be very large, return it **modulo** `10^9 + 7`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: word = "aabbccdd", k = 7 + +Output: 5 + +Explanation: + +The possible strings are: "aabbccdd", "aabbccd", "aabbcdd", "aabccdd", and "abbccdd". +``` + +**Example 2:** + ``` +Input: word = "aabbccdd", k = 8 -## 题意 -> ... +Output: 1 -## 题解 +Explanation: -### 思路1 -> ... -Find the Original Typed String II -```go +The only possible string is "aabbccdd". ``` +**Example 3:** + +``` +Input: word = "aaabbb", k = 3 + +Output: 8 +``` ## 结语 diff --git a/leetcode/3301-3400/3333.Find-the-Original-Typed-String-II/Solution.go b/leetcode/3301-3400/3333.Find-the-Original-Typed-String-II/Solution.go index d115ccf5..e3a7c995 100644 --- a/leetcode/3301-3400/3333.Find-the-Original-Typed-String-II/Solution.go +++ b/leetcode/3301-3400/3333.Find-the-Original-Typed-String-II/Solution.go @@ -1,5 +1,53 @@ package Solution -func Solution(x bool) bool { - return x +const mod = 1000000007 + +func Solution(word string, k int) int { + n := len(word) + cnt := 1 + var freq []int + + for i := 1; i < n; i++ { + if word[i] == word[i-1] { + cnt++ + } else { + freq = append(freq, cnt) + cnt = 1 + } + } + freq = append(freq, cnt) + + ans := 1 + for _, o := range freq { + ans = ans * o % mod + } + + if len(freq) >= k { + return ans + } + + f := make([]int, k) + g := make([]int, k) + f[0] = 1 + for i := range g { + g[i] = 1 + } + + for i := 0; i < len(freq); i++ { + f_new := make([]int, k) + for j := 1; j < k; j++ { + f_new[j] = g[j-1] + if j-freq[i]-1 >= 0 { + f_new[j] = (f_new[j] - g[j-freq[i]-1] + mod) % mod + } + } + g_new := make([]int, k) + g_new[0] = f_new[0] + for j := 1; j < k; j++ { + g_new[j] = (g_new[j-1] + f_new[j]) % mod + } + f, g = f_new, g_new + } + + return (ans - g[k-1] + mod) % mod } diff --git a/leetcode/3301-3400/3333.Find-the-Original-Typed-String-II/Solution_test.go b/leetcode/3301-3400/3333.Find-the-Original-Typed-String-II/Solution_test.go index 14ff50eb..b56c58ed 100644 --- a/leetcode/3301-3400/3333.Find-the-Original-Typed-String-II/Solution_test.go +++ b/leetcode/3301-3400/3333.Find-the-Original-Typed-String-II/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs string + k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "aabbccdd", 7, 5}, + {"TestCase2", "aabbccdd", 8, 1}, + {"TestCase3", "aaabbb", 3, 8}, } // 开始测试 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.k) 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.inputs, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }