Skip to content

Commit d9fbc0c

Browse files
committed
Sync LeetCode submission Runtime - 5892 ms (5.68%), Memory - 18.8 MB (70.45%)
1 parent 62b29f9 commit d9fbc0c

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<p>You are given a string <code>s</code> consisting of lowercase English letters, an integer <code>t</code> representing the number of <strong>transformations</strong> to perform, and an array <code>nums</code> of size 26. In one <strong>transformation</strong>, every character in <code>s</code> is replaced according to the following rules:</p>
2+
3+
<ul>
4+
<li>Replace <code>s[i]</code> with the <strong>next</strong> <code>nums[s[i] - &#39;a&#39;]</code> consecutive characters in the alphabet. For example, if <code>s[i] = &#39;a&#39;</code> and <code>nums[0] = 3</code>, the character <code>&#39;a&#39;</code> transforms into the next 3 consecutive characters ahead of it, which results in <code>&quot;bcd&quot;</code>.</li>
5+
<li>The transformation <strong>wraps</strong> around the alphabet if it exceeds <code>&#39;z&#39;</code>. For example, if <code>s[i] = &#39;y&#39;</code> and <code>nums[24] = 3</code>, the character <code>&#39;y&#39;</code> transforms into the next 3 consecutive characters ahead of it, which results in <code>&quot;zab&quot;</code>.</li>
6+
</ul>
7+
8+
<p>Return the length of the resulting string after <strong>exactly</strong> <code>t</code> transformations.</p>
9+
10+
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<div class="example-block">
16+
<p><strong>Input:</strong> <span class="example-io">s = &quot;abcyy&quot;, 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]</span></p>
17+
18+
<p><strong>Output:</strong> <span class="example-io">7</span></p>
19+
20+
<p><strong>Explanation:</strong></p>
21+
22+
<ul>
23+
<li>
24+
<p><strong>First Transformation (t = 1):</strong></p>
25+
26+
<ul>
27+
<li><code>&#39;a&#39;</code> becomes <code>&#39;b&#39;</code> as <code>nums[0] == 1</code></li>
28+
<li><code>&#39;b&#39;</code> becomes <code>&#39;c&#39;</code> as <code>nums[1] == 1</code></li>
29+
<li><code>&#39;c&#39;</code> becomes <code>&#39;d&#39;</code> as <code>nums[2] == 1</code></li>
30+
<li><code>&#39;y&#39;</code> becomes <code>&#39;z&#39;</code> as <code>nums[24] == 1</code></li>
31+
<li><code>&#39;y&#39;</code> becomes <code>&#39;z&#39;</code> as <code>nums[24] == 1</code></li>
32+
<li>String after the first transformation: <code>&quot;bcdzz&quot;</code></li>
33+
</ul>
34+
</li>
35+
<li>
36+
<p><strong>Second Transformation (t = 2):</strong></p>
37+
38+
<ul>
39+
<li><code>&#39;b&#39;</code> becomes <code>&#39;c&#39;</code> as <code>nums[1] == 1</code></li>
40+
<li><code>&#39;c&#39;</code> becomes <code>&#39;d&#39;</code> as <code>nums[2] == 1</code></li>
41+
<li><code>&#39;d&#39;</code> becomes <code>&#39;e&#39;</code> as <code>nums[3] == 1</code></li>
42+
<li><code>&#39;z&#39;</code> becomes <code>&#39;ab&#39;</code> as <code>nums[25] == 2</code></li>
43+
<li><code>&#39;z&#39;</code> becomes <code>&#39;ab&#39;</code> as <code>nums[25] == 2</code></li>
44+
<li>String after the second transformation: <code>&quot;cdeabab&quot;</code></li>
45+
</ul>
46+
</li>
47+
<li>
48+
<p><strong>Final Length of the string:</strong> The string is <code>&quot;cdeabab&quot;</code>, which has 7 characters.</p>
49+
</li>
50+
</ul>
51+
</div>
52+
53+
<p><strong class="example">Example 2:</strong></p>
54+
55+
<div class="example-block">
56+
<p><strong>Input:</strong> <span class="example-io">s = &quot;azbk&quot;, 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]</span></p>
57+
58+
<p><strong>Output:</strong> <span class="example-io">8</span></p>
59+
60+
<p><strong>Explanation:</strong></p>
61+
62+
<ul>
63+
<li>
64+
<p><strong>First Transformation (t = 1):</strong></p>
65+
66+
<ul>
67+
<li><code>&#39;a&#39;</code> becomes <code>&#39;bc&#39;</code> as <code>nums[0] == 2</code></li>
68+
<li><code>&#39;z&#39;</code> becomes <code>&#39;ab&#39;</code> as <code>nums[25] == 2</code></li>
69+
<li><code>&#39;b&#39;</code> becomes <code>&#39;cd&#39;</code> as <code>nums[1] == 2</code></li>
70+
<li><code>&#39;k&#39;</code> becomes <code>&#39;lm&#39;</code> as <code>nums[10] == 2</code></li>
71+
<li>String after the first transformation: <code>&quot;bcabcdlm&quot;</code></li>
72+
</ul>
73+
</li>
74+
<li>
75+
<p><strong>Final Length of the string:</strong> The string is <code>&quot;bcabcdlm&quot;</code>, which has 8 characters.</p>
76+
</li>
77+
</ul>
78+
</div>
79+
80+
<p>&nbsp;</p>
81+
<p><strong>Constraints:</strong></p>
82+
83+
<ul>
84+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
85+
<li><code>s</code> consists only of lowercase English letters.</li>
86+
<li><code>1 &lt;= t &lt;= 10<sup>9</sup></code></li>
87+
<li><code><font face="monospace">nums.length == 26</font></code></li>
88+
<li><code><font face="monospace">1 &lt;= nums[i] &lt;= 25</font></code></li>
89+
</ul>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
MOD = 10**9 + 7
2+
L = 26
3+
4+
5+
class Mat:
6+
def __init__(self, copy_from: "Mat" = None) -> None:
7+
self.a: List[List[int]] = [[0] * L for _ in range(L)]
8+
if copy_from:
9+
for i in range(L):
10+
for j in range(L):
11+
self.a[i][j] = copy_from.a[i][j]
12+
13+
def __mul__(self, other: "Mat") -> "Mat":
14+
result = Mat()
15+
for i in range(L):
16+
for j in range(L):
17+
for k in range(L):
18+
result.a[i][j] = (
19+
result.a[i][j] + self.a[i][k] * other.a[k][j]
20+
) % MOD
21+
return result
22+
23+
24+
# identity matrix
25+
def I() -> Mat:
26+
m = Mat()
27+
for i in range(L):
28+
m.a[i][i] = 1
29+
return m
30+
31+
32+
# matrix exponentiation by squaring
33+
def quickmul(x: Mat, y: int) -> Mat:
34+
ans = I()
35+
cur = x
36+
while y:
37+
if y & 1:
38+
ans = ans * cur
39+
cur = cur * cur
40+
y >>= 1
41+
return ans
42+
43+
44+
class Solution:
45+
def lengthAfterTransformations(
46+
self, s: str, t: int, nums: List[int]
47+
) -> int:
48+
T = Mat()
49+
for i in range(26):
50+
for j in range(1, nums[i] + 1):
51+
T.a[(i + j) % 26][i] = 1
52+
53+
res = quickmul(T, t)
54+
55+
f = [0] * 26
56+
for ch in s:
57+
f[ord(ch) - ord("a")] += 1
58+
59+
ans = 0
60+
for i in range(26):
61+
for j in range(26):
62+
ans = (ans + res.a[i][j] * f[j]) % MOD
63+
64+
return ans

0 commit comments

Comments
 (0)