Skip to content

Commit 62b29f9

Browse files
committed
Sync LeetCode submission Runtime - 2187 ms (64.05%), Memory - 18 MB (91.50%)
1 parent e508c59 commit 62b29f9

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<p>You are given a string <code>s</code> and an integer <code>t</code>, representing the number of <strong>transformations</strong> to perform. In one <strong>transformation</strong>, every character in <code>s</code> is replaced according to the following rules:</p>
2+
3+
<ul>
4+
<li>If the character is <code>&#39;z&#39;</code>, replace it with the string <code>&quot;ab&quot;</code>.</li>
5+
<li>Otherwise, replace it with the <strong>next</strong> character in the alphabet. For example, <code>&#39;a&#39;</code> is replaced with <code>&#39;b&#39;</code>, <code>&#39;b&#39;</code> is replaced with <code>&#39;c&#39;</code>, and so on.</li>
6+
</ul>
7+
8+
<p>Return the <strong>length</strong> 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><!-- notionvc: eb142f2b-b818-4064-8be5-e5a36b07557a --> <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</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><strong>First Transformation (t = 1)</strong>:
24+
25+
<ul>
26+
<li><code>&#39;a&#39;</code> becomes <code>&#39;b&#39;</code></li>
27+
<li><code>&#39;b&#39;</code> becomes <code>&#39;c&#39;</code></li>
28+
<li><code>&#39;c&#39;</code> becomes <code>&#39;d&#39;</code></li>
29+
<li><code>&#39;y&#39;</code> becomes <code>&#39;z&#39;</code></li>
30+
<li><code>&#39;y&#39;</code> becomes <code>&#39;z&#39;</code></li>
31+
<li>String after the first transformation: <code>&quot;bcdzz&quot;</code></li>
32+
</ul>
33+
</li>
34+
<li><strong>Second Transformation (t = 2)</strong>:
35+
<ul>
36+
<li><code>&#39;b&#39;</code> becomes <code>&#39;c&#39;</code></li>
37+
<li><code>&#39;c&#39;</code> becomes <code>&#39;d&#39;</code></li>
38+
<li><code>&#39;d&#39;</code> becomes <code>&#39;e&#39;</code></li>
39+
<li><code>&#39;z&#39;</code> becomes <code>&quot;ab&quot;</code></li>
40+
<li><code>&#39;z&#39;</code> becomes <code>&quot;ab&quot;</code></li>
41+
<li>String after the second transformation: <code>&quot;cdeabab&quot;</code></li>
42+
</ul>
43+
</li>
44+
<li><strong>Final Length of the string</strong>: The string is <code>&quot;cdeabab&quot;</code>, which has 7 characters.</li>
45+
</ul>
46+
</div>
47+
48+
<p><strong class="example">Example 2:</strong></p>
49+
50+
<div class="example-block">
51+
<p><strong>Input:</strong> <span class="example-io">s = &quot;azbk&quot;, t = 1</span></p>
52+
53+
<p><strong>Output:</strong> <span class="example-io">5</span></p>
54+
55+
<p><strong>Explanation:</strong></p>
56+
57+
<ul>
58+
<li><strong>First Transformation (t = 1)</strong>:
59+
60+
<ul>
61+
<li><code>&#39;a&#39;</code> becomes <code>&#39;b&#39;</code></li>
62+
<li><code>&#39;z&#39;</code> becomes <code>&quot;ab&quot;</code></li>
63+
<li><code>&#39;b&#39;</code> becomes <code>&#39;c&#39;</code></li>
64+
<li><code>&#39;k&#39;</code> becomes <code>&#39;l&#39;</code></li>
65+
<li>String after the first transformation: <code>&quot;babcl&quot;</code></li>
66+
</ul>
67+
</li>
68+
<li><strong>Final Length of the string</strong>: The string is <code>&quot;babcl&quot;</code>, which has 5 characters.</li>
69+
</ul>
70+
</div>
71+
72+
<p>&nbsp;</p>
73+
<p><strong>Constraints:</strong></p>
74+
75+
<ul>
76+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
77+
<li><code>s</code> consists only of lowercase English letters.</li>
78+
<li><code>1 &lt;= t &lt;= 10<sup>5</sup></code></li>
79+
</ul>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Approach: Recurrence
2+
3+
class Solution:
4+
def lengthAfterTransformations(self, s: str, t: int) -> int:
5+
MOD = 10 ** 9 + 7
6+
count = [0] * 26
7+
8+
for ch in s:
9+
count[ord(ch) - ord('a')] += 1
10+
11+
for round in range(t):
12+
nxt = [0] * 26
13+
nxt[0] = count[25]
14+
nxt[1] = (count[25] + count[0]) % MOD
15+
16+
for i in range(2, 26):
17+
nxt[i] = count[i - 1]
18+
19+
count = nxt
20+
21+
ans = sum(count) % MOD
22+
return ans
23+

0 commit comments

Comments
 (0)