Skip to content

Commit c21d5b4

Browse files
committed
Improved task 3337
1 parent 2e73f9e commit c21d5b4

File tree

2 files changed

+46
-61
lines changed
  • src/main/java/g3301_3400/s3337_total_characters_in_string_after_transformations_ii

2 files changed

+46
-61
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2265,7 +2265,7 @@
22652265
| 3342 |[Find Minimum Time to Reach Last Room II](src/main/java/g3301_3400/s3342_find_minimum_time_to_reach_last_room_ii)| Medium | Array, Matrix, Heap_Priority_Queue, Graph, Shortest_Path | 495 | 37.48
22662266
| 3341 |[Find Minimum Time to Reach Last Room I](src/main/java/g3301_3400/s3341_find_minimum_time_to_reach_last_room_i)| Medium | Array, Matrix, Heap_Priority_Queue, Graph, Shortest_Path | 8 | 67.58
22672267
| 3340 |[Check Balanced String](src/main/java/g3301_3400/s3340_check_balanced_string)| Easy | String | 1 | 99.60
2268-
| 3337 |[Total Characters in String After Transformations II](src/main/java/g3301_3400/s3337_total_characters_in_string_after_transformations_ii)| Hard | String, Hash_Table, Dynamic_Programming, Math, Counting | 67 | 99.31
2268+
| 3337 |[Total Characters in String After Transformations II](src/main/java/g3301_3400/s3337_total_characters_in_string_after_transformations_ii)| Hard | String, Hash_Table, Dynamic_Programming, Math, Counting | 80 | 72.97
22692269
| 3336 |[Find the Number of Subsequences With Equal GCD](src/main/java/g3301_3400/s3336_find_the_number_of_subsequences_with_equal_gcd)| Hard | Array, Dynamic_Programming, Math, Number_Theory | 408 | 50.28
22702270
| 3335 |[Total Characters in String After Transformations I](src/main/java/g3301_3400/s3335_total_characters_in_string_after_transformations_i)| Medium | String, Hash_Table, Dynamic_Programming, Math, Counting | 77 | 77.83
22712271
| 3334 |[Find the Maximum Factor Score of Array](src/main/java/g3301_3400/s3334_find_the_maximum_factor_score_of_array)| Medium | Array, Math, Number_Theory | 5 | 95.93

src/main/java/g3301_3400/s3337_total_characters_in_string_after_transformations_ii/readme.md

Lines changed: 45 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -75,83 +75,68 @@ Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> +
7575
import java.util.List;
7676

7777
public class Solution {
78-
public static final int MOD = 1000000007;
79-
public static final long M2 = (long) MOD * MOD;
80-
public static final long BIG = 8L * M2;
78+
private static final int MOD = 1_000_000_007;
8179

82-
public int lengthAfterTransformations(String s, int t, List<Integer> nums) {
83-
int[][] m = new int[26][26];
84-
for (int i = 0; i < 26; i++) {
85-
for (int j = 1; j <= nums.get(i); j++) {
86-
m[(i + j) % 26][i]++;
87-
}
88-
}
89-
int[] v = new int[26];
80+
public int lengthAfterTransformations(String s, int t, List<Integer> numsList) {
81+
int[][] localT = buildTransformationMatrix(numsList);
82+
int[][] tPower = matrixPower(localT, t);
83+
int[] freq = new int[26];
9084
for (char c : s.toCharArray()) {
91-
v[c - 'a']++;
85+
freq[c - 'a']++;
9286
}
93-
v = pow(m, v, t);
94-
long ans = 0;
95-
for (int x : v) {
96-
ans += x;
87+
long result = 0;
88+
for (int i = 0; i < 26; i++) {
89+
long sum = 0;
90+
for (int j = 0; j < 26; j++) {
91+
sum = (sum + (long) freq[j] * tPower[j][i]) % MOD;
92+
}
93+
result = (result + sum) % MOD;
9794
}
98-
return (int) (ans % MOD);
95+
96+
return (int) result;
9997
}
10098

101-
// A^e*v
102-
private int[] pow(int[][] a, int[] v, long e) {
103-
for (int i = 0; i < v.length; i++) {
104-
if (v[i] >= MOD) {
105-
v[i] %= MOD;
106-
}
107-
}
108-
int[][] mul = a;
109-
for (; e > 0; e >>>= 1) {
110-
if ((e & 1) == 1) {
111-
v = mul(mul, v);
99+
private int[][] buildTransformationMatrix(List<Integer> numsList) {
100+
int[][] localT = new int[26][26];
101+
for (int i = 0; i < 26; i++) {
102+
int steps = numsList.get(i);
103+
for (int j = 1; j <= steps; j++) {
104+
localT[i][(i + j) % 26]++;
112105
}
113-
mul = p2(mul);
114106
}
115-
return v;
107+
return localT;
116108
}
117109

118-
// int matrix*int vector
119-
private int[] mul(int[][] a, int[] v) {
120-
int m = a.length;
121-
int n = v.length;
122-
int[] w = new int[m];
123-
for (int i = 0; i < m; i++) {
124-
long sum = 0;
125-
for (int k = 0; k < n; k++) {
126-
sum += (long) a[i][k] * v[k];
127-
if (sum >= BIG) {
128-
sum -= BIG;
129-
}
110+
private int[][] matrixPower(int[][] matrix, int power) {
111+
int size = matrix.length;
112+
int[][] result = new int[size][size];
113+
for (int i = 0; i < size; i++) {
114+
result[i][i] = 1;
115+
}
116+
while (power > 0) {
117+
if ((power & 1) == 1) {
118+
result = multiplyMatrices(result, matrix);
130119
}
131-
w[i] = (int) (sum % MOD);
120+
matrix = multiplyMatrices(matrix, matrix);
121+
power >>= 1;
132122
}
133-
return w;
123+
return result;
134124
}
135125

136-
// int matrix^2 (be careful about negative value)
137-
private int[][] p2(int[][] a) {
138-
int n = a.length;
139-
int[][] c = new int[n][n];
140-
for (int i = 0; i < n; i++) {
141-
long[] sum = new long[n];
142-
for (int k = 0; k < n; k++) {
143-
for (int j = 0; j < n; j++) {
144-
sum[j] += (long) a[i][k] * a[k][j];
145-
if (sum[j] >= BIG) {
146-
sum[j] -= BIG;
147-
}
126+
private int[][] multiplyMatrices(int[][] a, int[][] b) {
127+
int size = a.length;
128+
int[][] result = new int[size][size];
129+
for (int i = 0; i < size; i++) {
130+
for (int k = 0; k < size; k++) {
131+
if (a[i][k] == 0) {
132+
continue;
133+
}
134+
for (int j = 0; j < size; j++) {
135+
result[i][j] = (int) ((result[i][j] + (long) a[i][k] * b[k][j]) % MOD);
148136
}
149-
}
150-
for (int j = 0; j < n; j++) {
151-
c[i][j] = (int) (sum[j] % MOD);
152137
}
153138
}
154-
return c;
139+
return result;
155140
}
156141
}
157142
```

0 commit comments

Comments
 (0)