Skip to content

Commit 011340d

Browse files
committed
Add solution and test-cases for problem 3337
1 parent a01e89c commit 011340d

File tree

3 files changed

+134
-25
lines changed

3 files changed

+134
-25
lines changed

leetcode/3301-3400/3337.Total-Characters-in-String-After-Transformations-II/README.md

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,61 @@
11
# [3337.Total Characters in String After Transformations II][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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:
5+
6+
- 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"`.
7+
- 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"`.
8+
9+
Return the length of the resulting string after **exactly** `t` transformations.
10+
11+
Since the answer may be very large, return it **modulo** `10^9 + 7`.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
16+
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]
17+
18+
Output: 7
19+
20+
Explanation:
1421
15-
## 题意
16-
> ...
22+
First Transformation (t = 1):
1723
18-
## 题解
24+
'a' becomes 'b' as nums[0] == 1
25+
'b' becomes 'c' as nums[1] == 1
26+
'c' becomes 'd' as nums[2] == 1
27+
'y' becomes 'z' as nums[24] == 1
28+
'y' becomes 'z' as nums[24] == 1
29+
String after the first transformation: "bcdzz"
30+
Second Transformation (t = 2):
1931
20-
### 思路1
21-
> ...
22-
Total Characters in String After Transformations II
23-
```go
32+
'b' becomes 'c' as nums[1] == 1
33+
'c' becomes 'd' as nums[2] == 1
34+
'd' becomes 'e' as nums[3] == 1
35+
'z' becomes 'ab' as nums[25] == 2
36+
'z' becomes 'ab' as nums[25] == 2
37+
String after the second transformation: "cdeabab"
38+
Final Length of the string: The string is "cdeabab", which has 7 characters.
2439
```
2540

41+
**Example 2:**
42+
43+
```
44+
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]
45+
46+
Output: 8
47+
48+
Explanation:
49+
50+
First Transformation (t = 1):
51+
52+
'a' becomes 'bc' as nums[0] == 2
53+
'z' becomes 'ab' as nums[25] == 2
54+
'b' becomes 'cd' as nums[1] == 2
55+
'k' becomes 'lm' as nums[10] == 2
56+
String after the first transformation: "bcabcdlm"
57+
Final Length of the string: The string is "bcabcdlm", which has 8 characters.
58+
```
2659

2760
## 结语
2861

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,80 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
const MOD = 1e9 + 7
4+
const L = 26
5+
6+
func Solution(s string, t int, nums []int) int {
7+
T := NewMat()
8+
for i := 0; i < L; i++ {
9+
for j := 1; j <= nums[i]; j++ {
10+
T.a[(i+j)%L][i] = 1
11+
}
12+
}
13+
14+
res := quickMul(T, t)
15+
f := make([]int, L)
16+
for _, ch := range s {
17+
f[ch-'a']++
18+
}
19+
20+
ans := 0
21+
for i := 0; i < L; i++ {
22+
for j := 0; j < L; j++ {
23+
ans = (ans + res.a[i][j]*f[j]) % MOD
24+
}
25+
}
26+
return ans
27+
}
28+
29+
type Mat struct {
30+
a [L][L]int
31+
}
32+
33+
func NewMat() *Mat {
34+
return &Mat{}
35+
}
36+
37+
func NewMatCopy(from *Mat) *Mat {
38+
m := &Mat{}
39+
for i := 0; i < L; i++ {
40+
for j := 0; j < L; j++ {
41+
m.a[i][j] = from.a[i][j]
42+
}
43+
}
44+
return m
45+
}
46+
47+
func (m *Mat) Mul(other *Mat) *Mat {
48+
result := NewMat()
49+
for i := 0; i < L; i++ {
50+
for j := 0; j < L; j++ {
51+
for k := 0; k < L; k++ {
52+
result.a[i][j] = (result.a[i][j] + m.a[i][k]*other.a[k][j]) % MOD
53+
}
54+
}
55+
}
56+
return result
57+
}
58+
59+
/* identity matrix */
60+
func I() *Mat {
61+
m := NewMat()
62+
for i := 0; i < L; i++ {
63+
m.a[i][i] = 1
64+
}
65+
return m
66+
}
67+
68+
/* matrix exponentiation by squaring */
69+
func quickMul(x *Mat, y int) *Mat {
70+
ans := I()
71+
cur := NewMatCopy(x)
72+
for y > 0 {
73+
if y&1 == 1 {
74+
ans = ans.Mul(cur)
75+
}
76+
cur = cur.Mul(cur)
77+
y >>= 1
78+
}
79+
return ans
580
}

leetcode/3301-3400/3337.Total-Characters-in-String-After-Transformations-II/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
tt int
15+
nums []int
16+
expect int
1517
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
18+
{"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},
19+
{"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},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.tt, c.nums)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.inputs, c.tt, c.nums)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)