diff --git a/leetcode/3301-3400/3337.Total-Characters-in-String-After-Transformations-II/README.md b/leetcode/3301-3400/3337.Total-Characters-in-String-After-Transformations-II/README.md index 56158ae4..5f9f58d0 100755 --- a/leetcode/3301-3400/3337.Total-Characters-in-String-After-Transformations-II/README.md +++ b/leetcode/3301-3400/3337.Total-Characters-in-String-After-Transformations-II/README.md @@ -1,28 +1,61 @@ # [3337.Total Characters in String After Transformations 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 +You are given a string `s` consisting of lowercase English letters, an integer `t` representing the number of **transformation** to perform, and an array `nums` of size 26. In one **transformation**, every character in s is replaced according to the following rules: + +- Replace `s[i]` with the **next** `nums[s[i] - 'a']` consecutive characters in the alphabet. For example, if `s[i] = 'a'` and `nums[0] = 3`, the character `'a'` transforms into the next 3 consecutive characters ahead of it, which results in `"bcd"`. +- The transformation **wraps** around the alphabet if it exceeds `'z'`. For example, if `s[i] = 'y'` and `nums[24] = 3`, the character `'y'` transforms into the next 3 consecutive characters ahead of it, which results in `"zab"`. + +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, nums = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2] + +Output: 7 + +Explanation: -## 题意 -> ... +First Transformation (t = 1): -## 题解 +'a' becomes 'b' as nums[0] == 1 +'b' becomes 'c' as nums[1] == 1 +'c' becomes 'd' as nums[2] == 1 +'y' becomes 'z' as nums[24] == 1 +'y' becomes 'z' as nums[24] == 1 +String after the first transformation: "bcdzz" +Second Transformation (t = 2): -### 思路1 -> ... -Total Characters in String After Transformations II -```go +'b' becomes 'c' as nums[1] == 1 +'c' becomes 'd' as nums[2] == 1 +'d' becomes 'e' as nums[3] == 1 +'z' becomes 'ab' as nums[25] == 2 +'z' becomes 'ab' as nums[25] == 2 +String after the second transformation: "cdeabab" +Final Length of the string: The string is "cdeabab", which has 7 characters. ``` +**Example 2:** + +``` +Input: s = "azbk", t = 1, nums = [2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2] + +Output: 8 + +Explanation: + +First Transformation (t = 1): + +'a' becomes 'bc' as nums[0] == 2 +'z' becomes 'ab' as nums[25] == 2 +'b' becomes 'cd' as nums[1] == 2 +'k' becomes 'lm' as nums[10] == 2 +String after the first transformation: "bcabcdlm" +Final Length of the string: The string is "bcabcdlm", which has 8 characters. +``` ## 结语 diff --git a/leetcode/3301-3400/3337.Total-Characters-in-String-After-Transformations-II/Solution.go b/leetcode/3301-3400/3337.Total-Characters-in-String-After-Transformations-II/Solution.go index d115ccf5..e60133b5 100644 --- a/leetcode/3301-3400/3337.Total-Characters-in-String-After-Transformations-II/Solution.go +++ b/leetcode/3301-3400/3337.Total-Characters-in-String-After-Transformations-II/Solution.go @@ -1,5 +1,80 @@ package Solution -func Solution(x bool) bool { - return x +const MOD = 1e9 + 7 +const L = 26 + +func Solution(s string, t int, nums []int) int { + T := NewMat() + for i := 0; i < L; i++ { + for j := 1; j <= nums[i]; j++ { + T.a[(i+j)%L][i] = 1 + } + } + + res := quickMul(T, t) + f := make([]int, L) + for _, ch := range s { + f[ch-'a']++ + } + + ans := 0 + for i := 0; i < L; i++ { + for j := 0; j < L; j++ { + ans = (ans + res.a[i][j]*f[j]) % MOD + } + } + return ans +} + +type Mat struct { + a [L][L]int +} + +func NewMat() *Mat { + return &Mat{} +} + +func NewMatCopy(from *Mat) *Mat { + m := &Mat{} + for i := 0; i < L; i++ { + for j := 0; j < L; j++ { + m.a[i][j] = from.a[i][j] + } + } + return m +} + +func (m *Mat) Mul(other *Mat) *Mat { + result := NewMat() + for i := 0; i < L; i++ { + for j := 0; j < L; j++ { + for k := 0; k < L; k++ { + result.a[i][j] = (result.a[i][j] + m.a[i][k]*other.a[k][j]) % MOD + } + } + } + return result +} + +/* identity matrix */ +func I() *Mat { + m := NewMat() + for i := 0; i < L; i++ { + m.a[i][i] = 1 + } + return m +} + +/* matrix exponentiation by squaring */ +func quickMul(x *Mat, y int) *Mat { + ans := I() + cur := NewMatCopy(x) + for y > 0 { + if y&1 == 1 { + ans = ans.Mul(cur) + } + cur = cur.Mul(cur) + y >>= 1 + } + return ans } diff --git a/leetcode/3301-3400/3337.Total-Characters-in-String-After-Transformations-II/Solution_test.go b/leetcode/3301-3400/3337.Total-Characters-in-String-After-Transformations-II/Solution_test.go index 14ff50eb..3567251c 100644 --- a/leetcode/3301-3400/3337.Total-Characters-in-String-After-Transformations-II/Solution_test.go +++ b/leetcode/3301-3400/3337.Total-Characters-in-String-After-Transformations-II/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs string + tt int + nums []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "abcyy", 2, []int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2}, 7}, + {"TestCase2", "azbk", 1, []int{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, 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.tt, c.nums) 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 %v", + c.expect, got, c.inputs, c.tt, c.nums) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }