Skip to content

Commit 3a72261

Browse files
authored
feat: add weekly contest 417 (#3583)
1 parent 44c60a5 commit 3a72261

File tree

27 files changed

+2099
-1
lines changed

27 files changed

+2099
-1
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3304.Find%20the%20K-th%20Character%20in%20String%20Game%20I/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3304. 找出第 K 个字符 I](https://leetcode.cn/problems/find-the-k-th-character-in-string-game-i)
10+
11+
[English Version](/solution/3300-3399/3304.Find%20the%20K-th%20Character%20in%20String%20Game%20I/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>Alice 和 Bob 正在玩一个游戏。最初,Alice 有一个字符串 <code>word = "a"</code>。</p>
18+
19+
<p>给定一个<strong>正整数</strong> <code>k</code>。</p>
20+
21+
<p>现在 Bob 会要求 Alice 执行以下操作<strong> 无限次 </strong>:</p>
22+
23+
<ul>
24+
<li>将 <code>word</code> 中的每个字符<strong> 更改 </strong>为英文字母表中的<strong> 下一个 </strong>字符来生成一个新字符串,并将其<strong> 追加 </strong>到原始的 <code>word</code>。</li>
25+
</ul>
26+
27+
<p>例如,对 <code>"c"</code> 进行操作生成 <code>"cd"</code>,对 <code>"zb"</code> 进行操作生成 <code>"zbac"</code>。</p>
28+
29+
<p>在执行足够多的操作后, <code>word</code> 中 <strong>至少 </strong>存在 <code>k</code> 个字符,此时返回 <code>word</code> 中第 <code>k</code> 个字符的值。</p>
30+
31+
<p><strong>注意</strong>,在操作中字符 <code>'z'</code> 可以变成 <code>'a'</code>。</p>
32+
33+
<p>&nbsp;</p>
34+
35+
<p><strong class="example">示例 1:</strong></p>
36+
37+
<div class="example-block">
38+
<p><strong>输入:</strong><span class="example-io">k = 5</span></p>
39+
40+
<p><strong>输出:</strong><span class="example-io">"b"</span></p>
41+
42+
<p><strong>解释:</strong></p>
43+
44+
<p>最初,<code>word = "a"</code>。需要进行三次操作:</p>
45+
46+
<ul>
47+
<li>生成的字符串是 <code>"b"</code>,<code>word</code> 变为 <code>"ab"</code>。</li>
48+
<li>生成的字符串是 <code>"bc"</code>,<code>word</code> 变为 <code>"abbc"</code>。</li>
49+
<li>生成的字符串是 <code>"bccd"</code>,<code>word</code> 变为 <code>"abbcbccd"</code>。</li>
50+
</ul>
51+
</div>
52+
53+
<p><strong class="example">示例 2:</strong></p>
54+
55+
<div class="example-block">
56+
<p><strong>输入:</strong><span class="example-io">k = 10</span></p>
57+
58+
<p><strong>输出:</strong><span class="example-io">"c"</span></p>
59+
</div>
60+
61+
<p>&nbsp;</p>
62+
63+
<p><strong>提示:</strong></p>
64+
65+
<ul>
66+
<li><code>1 &lt;= k &lt;= 500</code></li>
67+
</ul>
68+
69+
<!-- description:end -->
70+
71+
## 解法
72+
73+
<!-- solution:start -->
74+
75+
### 方法一:模拟
76+
77+
我们可以使用一个数组 $\textit{word}$ 来存储每次操作后的字符串,当 $\textit{word}$ 的长度小于 $k$ 时,我们不断地对 $\textit{word}$ 进行操作。
78+
79+
最后返回 $\textit{word}[k - 1]$ 即可。
80+
81+
时间复杂度 $O(k)$,空间复杂度 $O(k)$。其中 $k$ 为输入参数。
82+
83+
<!-- tabs:start -->
84+
85+
#### Python3
86+
87+
```python
88+
class Solution:
89+
def kthCharacter(self, k: int) -> str:
90+
word = [0]
91+
while len(word) < k:
92+
word.extend([(x + 1) % 26 for x in word])
93+
return chr(ord("a") + word[k - 1])
94+
```
95+
96+
#### Java
97+
98+
```java
99+
class Solution {
100+
public char kthCharacter(int k) {
101+
List<Integer> word = new ArrayList<>();
102+
word.add(0);
103+
while (word.size() < k) {
104+
for (int i = 0, m = word.size(); i < m; ++i) {
105+
word.add((word.get(i) + 1) % 26);
106+
}
107+
}
108+
return (char) ('a' + word.get(k - 1));
109+
}
110+
}
111+
```
112+
113+
#### C++
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
char kthCharacter(int k) {
119+
vector<int> word;
120+
word.push_back(0);
121+
while (word.size() < k) {
122+
int m = word.size();
123+
for (int i = 0; i < m; ++i) {
124+
word.push_back((word[i] + 1) % 26);
125+
}
126+
}
127+
return 'a' + word[k - 1];
128+
}
129+
};
130+
```
131+
132+
#### Go
133+
134+
```go
135+
func kthCharacter(k int) byte {
136+
word := []int{0}
137+
for len(word) < k {
138+
m := len(word)
139+
for i := 0; i < m; i++ {
140+
word = append(word, (word[i]+1)%26)
141+
}
142+
}
143+
return 'a' + byte(word[k-1])
144+
}
145+
```
146+
147+
#### TypeScript
148+
149+
```ts
150+
function kthCharacter(k: number): string {
151+
const word: number[] = [0];
152+
while (word.length < k) {
153+
word.push(...word.map(x => (x + 1) % 26));
154+
}
155+
return String.fromCharCode(97 + word[k - 1]);
156+
}
157+
```
158+
159+
<!-- tabs:end -->
160+
161+
<!-- solution:end -->
162+
163+
<!-- problem:end -->
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3304.Find%20the%20K-th%20Character%20in%20String%20Game%20I/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3304. Find the K-th Character in String Game I](https://leetcode.com/problems/find-the-k-th-character-in-string-game-i)
10+
11+
[中文文档](/solution/3300-3399/3304.Find%20the%20K-th%20Character%20in%20String%20Game%20I/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>Alice and Bob are playing a game. Initially, Alice has a string <code>word = &quot;a&quot;</code>.</p>
18+
19+
<p>You are given a <strong>positive</strong> integer <code>k</code>.</p>
20+
21+
<p>Now Bob will ask Alice to perform the following operation <strong>forever</strong>:</p>
22+
23+
<ul>
24+
<li>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>.</li>
25+
</ul>
26+
27+
<p>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>.</p>
28+
29+
<p>Return the value of the <code>k<sup>th</sup></code> character in <code>word</code>, after enough operations have been done for <code>word</code> to have <strong>at least</strong> <code>k</code> characters.</p>
30+
31+
<p><strong>Note</strong> that the character <code>&#39;z&#39;</code> can be changed to <code>&#39;a&#39;</code> in the operation.</p>
32+
33+
<p>&nbsp;</p>
34+
<p><strong class="example">Example 1:</strong></p>
35+
36+
<div class="example-block">
37+
<p><strong>Input:</strong> <span class="example-io">k = 5</span></p>
38+
39+
<p><strong>Output:</strong> <span class="example-io">&quot;b&quot;</span></p>
40+
41+
<p><strong>Explanation:</strong></p>
42+
43+
<p>Initially, <code>word = &quot;a&quot;</code>. We need to do the operation three times:</p>
44+
45+
<ul>
46+
<li>Generated string is <code>&quot;b&quot;</code>, <code>word</code> becomes <code>&quot;ab&quot;</code>.</li>
47+
<li>Generated string is <code>&quot;bc&quot;</code>, <code>word</code> becomes <code>&quot;abbc&quot;</code>.</li>
48+
<li>Generated string is <code>&quot;bccd&quot;</code>, <code>word</code> becomes <code>&quot;abbcbccd&quot;</code>.</li>
49+
</ul>
50+
</div>
51+
52+
<p><strong class="example">Example 2:</strong></p>
53+
54+
<div class="example-block">
55+
<p><strong>Input:</strong> <span class="example-io">k = 10</span></p>
56+
57+
<p><strong>Output:</strong> <span class="example-io">&quot;c&quot;</span></p>
58+
</div>
59+
60+
<p>&nbsp;</p>
61+
<p><strong>Constraints:</strong></p>
62+
63+
<ul>
64+
<li><code>1 &lt;= k &lt;= 500</code></li>
65+
</ul>
66+
67+
<!-- description:end -->
68+
69+
## Solutions
70+
71+
<!-- solution:start -->
72+
73+
### Solution 1: Simulation
74+
75+
We can use an array $\textit{word}$ to store the string after each operation. When the length of $\textit{word}$ is less than $k$, we continuously perform operations on $\textit{word}$.
76+
77+
Finally, return $\textit{word}[k - 1]$.
78+
79+
The time complexity is $O(k)$, and the space complexity is $O(k)$. Here, $k$ is the input parameter.
80+
81+
<!-- tabs:start -->
82+
83+
#### Python3
84+
85+
```python
86+
class Solution:
87+
def kthCharacter(self, k: int) -> str:
88+
word = [0]
89+
while len(word) < k:
90+
word.extend([(x + 1) % 26 for x in word])
91+
return chr(ord("a") + word[k - 1])
92+
```
93+
94+
#### Java
95+
96+
```java
97+
class Solution {
98+
public char kthCharacter(int k) {
99+
List<Integer> word = new ArrayList<>();
100+
word.add(0);
101+
while (word.size() < k) {
102+
for (int i = 0, m = word.size(); i < m; ++i) {
103+
word.add((word.get(i) + 1) % 26);
104+
}
105+
}
106+
return (char) ('a' + word.get(k - 1));
107+
}
108+
}
109+
```
110+
111+
#### C++
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
char kthCharacter(int k) {
117+
vector<int> word;
118+
word.push_back(0);
119+
while (word.size() < k) {
120+
int m = word.size();
121+
for (int i = 0; i < m; ++i) {
122+
word.push_back((word[i] + 1) % 26);
123+
}
124+
}
125+
return 'a' + word[k - 1];
126+
}
127+
};
128+
```
129+
130+
#### Go
131+
132+
```go
133+
func kthCharacter(k int) byte {
134+
word := []int{0}
135+
for len(word) < k {
136+
m := len(word)
137+
for i := 0; i < m; i++ {
138+
word = append(word, (word[i]+1)%26)
139+
}
140+
}
141+
return 'a' + byte(word[k-1])
142+
}
143+
```
144+
145+
#### TypeScript
146+
147+
```ts
148+
function kthCharacter(k: number): string {
149+
const word: number[] = [0];
150+
while (word.length < k) {
151+
word.push(...word.map(x => (x + 1) % 26));
152+
}
153+
return String.fromCharCode(97 + word[k - 1]);
154+
}
155+
```
156+
157+
<!-- tabs:end -->
158+
159+
<!-- solution:end -->
160+
161+
<!-- problem:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
char kthCharacter(int k) {
4+
vector<int> word;
5+
word.push_back(0);
6+
while (word.size() < k) {
7+
int m = word.size();
8+
for (int i = 0; i < m; ++i) {
9+
word.push_back((word[i] + 1) % 26);
10+
}
11+
}
12+
return 'a' + word[k - 1];
13+
}
14+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func kthCharacter(k int) byte {
2+
word := []int{0}
3+
for len(word) < k {
4+
m := len(word)
5+
for i := 0; i < m; i++ {
6+
word = append(word, (word[i]+1)%26)
7+
}
8+
}
9+
return 'a' + byte(word[k-1])
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public char kthCharacter(int k) {
3+
List<Integer> word = new ArrayList<>();
4+
word.add(0);
5+
while (word.size() < k) {
6+
for (int i = 0, m = word.size(); i < m; ++i) {
7+
word.add((word.get(i) + 1) % 26);
8+
}
9+
}
10+
return (char) ('a' + word.get(k - 1));
11+
}
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def kthCharacter(self, k: int) -> str:
3+
word = [0]
4+
while len(word) < k:
5+
word.extend([(x + 1) % 26 for x in word])
6+
return chr(ord("a") + word[k - 1])
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function kthCharacter(k: number): string {
2+
const word: number[] = [0];
3+
while (word.length < k) {
4+
word.push(...word.map(x => (x + 1) % 26));
5+
}
6+
return String.fromCharCode(97 + word[k - 1]);
7+
}

0 commit comments

Comments
 (0)