-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathtotal-characters-in-string-after-transformations-i.cpp
135 lines (126 loc) · 3.82 KB
/
total-characters-in-string-after-transformations-i.cpp
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
// Time: precompute: O(t + 26)
// runtime: O(n)
// Space: O(t + 26)
// precompute, dp
const int MOD = 1e9 + 7;
const int MAX_T = 1e5;
vector<int> init() {
vector<int> DP(MAX_T + 26);
for (int i = 0; i < 26; ++i) {
DP[i] = 1;
}
for (int i = 26; i < size(DP); ++i) {
DP[i] = (DP[i - 26] + DP[(i - 26) + 1]) % MOD;
}
return DP;
}
const auto& DP = init();
class Solution {
public:
int lengthAfterTransformations(string s, int t) {
int result = 0;
for (const auto& x : s) {
result = (result + DP[(x - 'a') + t]) % MOD;
}
return result;
}
};
// Time: O(n + t + 26)
// Space: O(26)
// dp
class Solution2 {
public:
int lengthAfterTransformations(string s, int t) {
static const int MOD = 1e9 + 7;
vector<int> dp(26, 1);
const auto mx = ranges::max(s);
for (int i = 26; i <= (mx - 'a') + t; ++i) {
dp[i % 26] = (dp[(i - 26) % 26] + dp[((i - 26) + 1) % 26]) % MOD;
}
int result = 0;
for (const auto& x : s) {
result = (result + dp[((x - 'a') + t) % 26]) % MOD;
}
return result;
}
};
// Time: O(n + 26^3 * logt)
// Space: O(26^2)
// matrix fast exponentiation
class Solution3 {
public:
int lengthAfterTransformations(string s, int t) {
vector<int> cnt(26);
for (const auto& x : s) {
++cnt[x - 'a'];
}
vector<int> nums(26, 1);
nums.back() = 2;
vector<vector<int>> matrix(26, vector<int>(26));
for (int i = 0; i < size(nums); ++i) {
for (int j = 1; j <= nums[i]; ++j) {
matrix[i][(i + j) % 26] = 1;
}
}
const auto& matrix_pow_t = matrixExpo(matrix, t);
const auto& result = matrixMult(vector<vector<int>>{{cnt}}, matrix_pow_t);
return accumulate(cbegin(result[0]), cend(result[0]), 0, [](const auto& accu, const auto& x) {
return (accu + x) % MOD;
});
}
private:
vector<vector<int>> matrixExpo(const vector<vector<int>>& A, int64_t pow) {
vector<vector<int>> result(A.size(), vector<int>(A.size()));
vector<vector<int>> A_exp(A);
for (int i = 0; i < A.size(); ++i) {
result[i][i] = 1;
}
while (pow) {
if (pow % 2 == 1) {
result = matrixMult(result, A_exp);
}
A_exp = matrixMult(A_exp, A_exp);
pow /= 2;
}
return result;
}
vector<vector<int>> matrixMult(const vector<vector<int>>& A, const vector<vector<int>>& B) {
vector<vector<int>> result(A.size(), vector<int>(B[0].size()));
for (int i = 0; i < A.size(); ++i) {
for (int j = 0; j < B[0].size(); ++j) {
int64_t entry = 0;
for (int k = 0; k < B.size(); ++k) {
entry = (static_cast<int64_t>(A[i][k]) * B[k][j] % MOD + entry) % MOD;
}
result[i][j] = static_cast<int>(entry);
}
}
return result;
}
static const int MOD = 1e9 + 7;
};
// Time: O(n + t * 26)
// Space: O(26)
// dp
class Solution4 {
public:
int lengthAfterTransformations(string s, int t) {
vector<int> cnt(26);
for (const auto& x : s) {
++cnt[x - 'a'];
}
for (int _ = 0; _ < t; ++_) {
vector<int> new_cnt(26);
for (int i = 0; i < 26; ++i) {
new_cnt[(i + 1) % 26] = (new_cnt[(i + 1) % 26] + cnt[i]) % MOD;
if (i == 25) {
new_cnt[(i + 2) % 26] = (new_cnt[(i + 2) % 26] + cnt[i]) % MOD;
}
}
cnt = move(new_cnt);
}
return accumulate(cbegin(cnt), cend(cnt), 0, [](const auto& accu, const auto& x) {
return (accu + x) % MOD;
});
}
};