-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
199 lines (181 loc) · 5.18 KB
/
main.go
File metadata and controls
199 lines (181 loc) · 5.18 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
// Source: https://leetcode.com/problems/total-characters-in-string-after-transformations-ii
// Title: Total Characters in String After Transformations II
// Difficulty: Hard
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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:
//
// - 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: 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):**
// - `'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.
//
// **Constraints:**
//
// - `1 <= s.length <= 10^5`
// - `s` consists only of lowercase English letters.
// - `1 <= t <= 10^9`
// - `nums.length == 26`
// - `1 <= nums[i] <= 25`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
const modulo = int(1e9 + 7)
// TLE
func lengthAfterTransformations(s string, t int, nums []int) int {
curr := [26]int{}
next := [26]int{}
for _, ch := range s {
curr[ch-'a']++
}
for range t {
for i := range 26 {
next[i] = 0
}
for i, num := range nums {
for j := range num {
next[(i+j+1)%26] += curr[i]
next[(i+j+1)%26] %= modulo
}
}
curr, next = next, curr
}
ans := 0
for _, count := range curr {
ans += count
ans %= modulo
}
return ans
}
// Markov chain, TLE
func lengthAfterTransformations2(s string, t int, nums []int) int {
curr := [26]int{}
for _, ch := range s {
curr[ch-'a']++
}
matrix := [26][26]int{}
for i, num := range nums {
for j := range num {
matrix[(i+j+1)%26][i] = 1
}
}
next := [26]int{}
for range t {
for i, row := range matrix {
next[i] = 0
for j, val := range row {
next[i] += curr[j] * val
next[i] %= modulo
}
}
curr, next = next, curr
}
ans := 0
for _, count := range curr {
ans += count
ans %= modulo
}
return ans
}
// Markov chain, compute matrix power
func lengthAfterTransformations3(s string, t int, nums []int) int {
vector := [26]int{}
for _, ch := range s {
vector[ch-'a']++
}
// Markov matrix
matrix := [26][26]int{}
for i, num := range nums {
for j := range num {
matrix[(i+j+1)%26][i] = 1
}
}
// Operators
gemm := func(a, b, c *[26][26]int) { // c = a * b
for i := range 26 {
for j := range 26 {
c[i][j] = 0
for k := range 26 {
c[i][j] += a[i][k] * b[k][j]
c[i][j] %= modulo
}
}
}
}
gemm2 := func(a, c *[26][26]int) { // c = a * a
for i := range 26 {
for j := range 26 {
c[i][j] = 0
for k := range 26 {
c[i][j] += a[i][k] * a[k][j]
c[i][j] %= modulo
}
}
}
}
// Fast matrix power
result := [26][26]int{}
temp := [26][26]int{}
for i := range 26 {
result[i][i] = 1
}
for true {
if (t & 1) != 0 {
gemm(&matrix, &result, &temp) // result *= matrix
result, temp = temp, result
}
t >>= 1
if t == 0 {
break
}
gemm2(&matrix, &temp) // matrix *= matrix
matrix, temp = temp, matrix
}
// Compute matrix vector multiplication
ans := 0
for _, row := range result {
for j, v := range row {
ans += v * vector[j]
ans %= modulo
}
}
return ans
}