diff --git a/leetcode/3301-3400/3335.Total-Characters-in-String-After-Transformations-I/README.md b/leetcode/3301-3400/3335.Total-Characters-in-String-After-Transformations-I/README.md index 9f43350bb..aa58e0834 100755 --- a/leetcode/3301-3400/3335.Total-Characters-in-String-After-Transformations-I/README.md +++ b/leetcode/3301-3400/3335.Total-Characters-in-String-After-Transformations-I/README.md @@ -1,28 +1,58 @@ # [3335.Total Characters in String After Transformations I][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 a string `s` and an integer `t`, representing the number of **transformations** to perform. In one **transformation**, every character in `s` is replaced according to the following rules: + +- If the character is `'z'`, replace it with the string `"ab"`. +- Otherwise, replace it with the next character in the alphabet. For example, `'a'` is replaced with `'b'`, `'b'` is replaced with `'c'`, and so on. + +Return the **length** of the resulting string after **exactly** `t` transformations. + +Since the answer may be very large, return it **modulo** `10^9 + 7`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "abcyy", t = 2 + +Output: 7 + +Explanation: + +First Transformation (t = 1): +'a' becomes 'b' +'b' becomes 'c' +'c' becomes 'd' +'y' becomes 'z' +'y' becomes 'z' +String after the first transformation: "bcdzz" +Second Transformation (t = 2): +'b' becomes 'c' +'c' becomes 'd' +'d' becomes 'e' +'z' becomes "ab" +'z' becomes "ab" +String after the second transformation: "cdeabab" +Final Length of the string: The string is "cdeabab", which has 7 characters. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Total Characters in String After Transformations I -```go ``` +Input: s = "azbk", t = 1 + +Output: 5 +Explanation: + +First Transformation (t = 1): +'a' becomes 'b' +'z' becomes "ab" +'b' becomes 'c' +'k' becomes 'l' +String after the first transformation: "babcl" +Final Length of the string: The string is "babcl", which has 5 characters. +``` ## 结语 diff --git a/leetcode/3301-3400/3335.Total-Characters-in-String-After-Transformations-I/Solution.go b/leetcode/3301-3400/3335.Total-Characters-in-String-After-Transformations-I/Solution.go index d115ccf5e..2ac4c61f5 100644 --- a/leetcode/3301-3400/3335.Total-Characters-in-String-After-Transformations-I/Solution.go +++ b/leetcode/3301-3400/3335.Total-Characters-in-String-After-Transformations-I/Solution.go @@ -1,5 +1,29 @@ package Solution -func Solution(x bool) bool { - return x +const mod = 1000000007 + +func Solution(s string, t int) int { + count := [26]int{} + for _, b := range s { + count[b-'a']++ + } + for ; t > 0; t-- { + tmp := [26]int{} + // a, b, c, d .... z + // count是前一轮的变化 + z := count[25] + for i := 25; i > 0; i-- { + tmp[i] = count[i-1] + } + tmp[0] = z + tmp[1] = (tmp[1] + z) % mod + for i := range 26 { + count[i] = tmp[i] + } + } + ans := 0 + for i := range 26 { + ans = (ans + count[i]) % mod + } + return ans } diff --git a/leetcode/3301-3400/3335.Total-Characters-in-String-After-Transformations-I/Solution_test.go b/leetcode/3301-3400/3335.Total-Characters-in-String-After-Transformations-I/Solution_test.go index 14ff50eb4..4cfca01dd 100644 --- a/leetcode/3301-3400/3335.Total-Characters-in-String-After-Transformations-I/Solution_test.go +++ b/leetcode/3301-3400/3335.Total-Characters-in-String-After-Transformations-I/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + s string + tt int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "abcyy", 2, 7}, + {"TestCase2", "azbk", 1, 5}, + {"TestCase3", "jqktcurgdvlibczdsvnsg", 7517, 79033769}, } // 开始测试 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.tt) 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.tt) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }