Skip to content

Commit 84e3d37

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 18 MB (30.97%)
1 parent 5633cd9 commit 84e3d37

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<p>Alice and Bob are playing a game. Initially, Alice has a string <code>word = &quot;a&quot;</code>.</p>
2+
3+
<p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given an integer array <code>operations</code>, where <code>operations[i]</code> represents the <strong>type</strong> of the <code>i<sup>th</sup></code> operation.</p>
4+
5+
<p>Now Bob will ask Alice to perform <strong>all</strong> operations in sequence:</p>
6+
7+
<ul>
8+
<li>If <code>operations[i] == 0</code>, <strong>append</strong> a copy of <code>word</code> to itself.</li>
9+
<li>If <code>operations[i] == 1</code>, generate a new string by <strong>changing</strong> each character in <code>word</code> to its <strong>next</strong> character in the English alphabet, and <strong>append</strong> it to the <em>original</em> <code>word</code>. For example, performing the operation on <code>&quot;c&quot;</code> generates <code>&quot;cd&quot;</code> and performing the operation on <code>&quot;zb&quot;</code> generates <code>&quot;zbac&quot;</code>.</li>
10+
</ul>
11+
12+
<p>Return the value of the <code>k<sup>th</sup></code> character in <code>word</code> after performing all the operations.</p>
13+
14+
<p><strong>Note</strong> that the character <code>&#39;z&#39;</code> can be changed to <code>&#39;a&#39;</code> in the second type of operation.</p>
15+
16+
<p>&nbsp;</p>
17+
<p><strong class="example">Example 1:</strong></p>
18+
19+
<div class="example-block">
20+
<p><strong>Input:</strong> <span class="example-io">k = 5, operations = [0,0,0]</span></p>
21+
22+
<p><strong>Output:</strong> <span class="example-io">&quot;a&quot;</span></p>
23+
24+
<p><strong>Explanation:</strong></p>
25+
26+
<p>Initially, <code>word == &quot;a&quot;</code>. Alice performs the three operations as follows:</p>
27+
28+
<ul>
29+
<li>Appends <code>&quot;a&quot;</code> to <code>&quot;a&quot;</code>, <code>word</code> becomes <code>&quot;aa&quot;</code>.</li>
30+
<li>Appends <code>&quot;aa&quot;</code> to <code>&quot;aa&quot;</code>, <code>word</code> becomes <code>&quot;aaaa&quot;</code>.</li>
31+
<li>Appends <code>&quot;aaaa&quot;</code> to <code>&quot;aaaa&quot;</code>, <code>word</code> becomes <code>&quot;aaaaaaaa&quot;</code>.</li>
32+
</ul>
33+
</div>
34+
35+
<p><strong class="example">Example 2:</strong></p>
36+
37+
<div class="example-block">
38+
<p><strong>Input:</strong> <span class="example-io">k = 10, operations = [0,1,0,1]</span></p>
39+
40+
<p><strong>Output:</strong> <span class="example-io">&quot;b&quot;</span></p>
41+
42+
<p><strong>Explanation:</strong></p>
43+
44+
<p>Initially, <code>word == &quot;a&quot;</code>. Alice performs the four operations as follows:</p>
45+
46+
<ul>
47+
<li>Appends <code>&quot;a&quot;</code> to <code>&quot;a&quot;</code>, <code>word</code> becomes <code>&quot;aa&quot;</code>.</li>
48+
<li>Appends <code>&quot;bb&quot;</code> to <code>&quot;aa&quot;</code>, <code>word</code> becomes <code>&quot;aabb&quot;</code>.</li>
49+
<li>Appends <code>&quot;aabb&quot;</code> to <code>&quot;aabb&quot;</code>, <code>word</code> becomes <code>&quot;aabbaabb&quot;</code>.</li>
50+
<li>Appends <code>&quot;bbccbbcc&quot;</code> to <code>&quot;aabbaabb&quot;</code>, <code>word</code> becomes <code>&quot;aabbaabbbbccbbcc&quot;</code>.</li>
51+
</ul>
52+
</div>
53+
54+
<p>&nbsp;</p>
55+
<p><strong>Constraints:</strong></p>
56+
57+
<ul>
58+
<li><code>1 &lt;= k &lt;= 10<sup>14</sup></code></li>
59+
<li><code>1 &lt;= operations.length &lt;= 100</code></li>
60+
<li><code>operations[i]</code> is either 0 or 1.</li>
61+
<li>The input is generated such that <code>word</code> has <strong>at least</strong> <code>k</code> characters after all operations.</li>
62+
</ul>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def kthCharacter(self, k: int, operations: List[int]) -> str:
3+
ans = 0
4+
while k != 1:
5+
t = k.bit_length() - 1
6+
if (1 << t) == k:
7+
t -= 1
8+
k -= 1 << t
9+
if operations[t]:
10+
ans += 1
11+
return chr(ord("a") + (ans % 26))

0 commit comments

Comments
 (0)