-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3337-TotalCharactersInStringAfterTransformationsII.go
More file actions
213 lines (196 loc) · 6.93 KB
/
3337-TotalCharactersInStringAfterTransformationsII.go
File metadata and controls
213 lines (196 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
// 3337. Total Characters in String After Transformations II
// You are given a string s consisting of lowercase English letters,
// an integer t representing the number of transformations to perform, and an array nums of size 26.
// In one transformation, every character in s is replaced according to the following rules:
// 1. 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".
// 2. 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 109 + 7.
// Example 1:
// 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:
// 1. 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"
// 2. Second Transformation (t = 2):
// '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"
// 3. 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:
// 1. 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"
// 2. Final Length of the string: The string is "bcabcdlm", which has 8 characters.
// Constraints:
// 1 <= s.length <= 10^5
// s consists only of lowercase English letters.
// 1 <= t <= 10^9
// nums.length == 26
// 1 <= nums[i] <= 25
import "fmt"
const mod = 1_000_000_007
type Matrix [][]int
func newMatrix(n, m int) Matrix {
arr := make(Matrix, n)
for i := range arr {
arr[i] = make([]int, m)
}
return arr
}
func (a Matrix) mul(b Matrix) Matrix {
c := newMatrix(len(a), len(b[0]))
for i, row := range a {
for k, x := range row {
if x == 0 { continue }
for j, y := range b[k] {
c[i][j] = (c[i][j] + x * y) % mod
}
}
}
return c
}
// a^n * f0
func (a Matrix) powMul(n int, f0 Matrix) Matrix {
res := f0
for ; n > 0; n /= 2 {
if n%2 > 0 {
res = a.mul(res)
}
a = a.mul(a)
}
return res
}
func lengthAfterTransformations(s string, t int, nums []int) int {
const size = 26
f0 := newMatrix(size, 1)
for i := range f0 {
f0[i][0] = 1
}
m := newMatrix(size, size)
for i, c := range nums {
for j := i + 1; j <= i+c; j++ {
m[i][j%size] = 1
}
}
m = m.powMul(t, f0)
res, cnt := 0, [26]int{}
for _, c := range s {
cnt[c-'a']++
}
for i, row := range m {
res += row[0] * cnt[i]
}
return res % mod
}
func lengthAfterTransformations1(s string, t int, nums []int) int {
res, mod := 0, 1_000_000_007
count := make([]int, 26)
for _, v := range s {
count[v-'a']++
}
matrix := make([][]int, 26)
for i := range matrix {
matrix[i] = make([]int, 26)
}
for i := 0; i < 26; i++ {
for j := 0; j < nums[i]; j++ {
matrix[(i + j + 1) % 26][i]++
}
}
matrixMultiple := func(a, b [][]int) [][]int {
res := make([][]int, 26)
for i := range res {
res[i] = make([]int, 26)
}
for i := 0; i < 26; i++ {
for j := 0; j < 26; j++ {
if a[i][j] != 0 {
for k := 0; k < 26; k++ {
res[i][k] = (res[i][k] + a[i][j] * b[j][k]) % mod
}
}
}
}
return res
}
matrixPow := func(a [][]int, n int) [][]int {
res := make([][]int, 26)
for i := range res {
res[i] = make([]int, 26)
res[i][i] = 1
}
for n > 0 {
if n % 2 == 1 {
res = matrixMultiple(a, res)
}
a = matrixMultiple(a, a)
n /= 2
}
return res
}
nm := matrixPow(matrix, t)
count1 := make([]int, 26)
for i := 0; i < 26; i++ {
for j := 0; j < 26; j++ {
count1[i] = (count1[i] + nm[i][j] * count[j]) % mod
}
}
for _, v := range count1 {
res = (res + v) % mod
}
return res
}
func main() {
// Example 1:
// 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:
// 1. 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"
// 2. Second Transformation (t = 2):
// '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"
// 3. Final Length of the string: The string is "cdeabab", which has 7 characters.
fmt.Println(lengthAfterTransformations("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
// 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:
// 1. 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"
// 2. Final Length of the string: The string is "bcabcdlm", which has 8 characters.
fmt.Println(lengthAfterTransformations("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
fmt.Println(lengthAfterTransformations1("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
fmt.Println(lengthAfterTransformations1("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
}