Skip to content

Commit 5a5adf3

Browse files
committed
+ problem 880
1 parent ae6bbfc commit 5a5adf3

File tree

5 files changed

+202
-0
lines changed

5 files changed

+202
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 880. Decoded String at Index
2+
You are given an encoded string `s`. To decode the string to a tape, the encoded string is read one character at a time and the following steps are taken:
3+
4+
* If the character read is a letter, that letter is written onto the tape.
5+
* If the character read is a digit `d`, the entire current tape is repeatedly written `d - 1` more times in total.
6+
7+
Given an integer `k`, return *the* <code>k<sup>th</sup></code> *letter (**1-indexed**) in the decoded string*.
8+
9+
#### Example 1:
10+
<pre>
11+
<strong>Input:</strong> s = "leet2code3", k = 10
12+
<strong>Output:</strong> "o"
13+
<strong>Explanation:</strong> The decoded string is "leetleetcodeleetleetcodeleetleetcode".
14+
The 10th letter in the string is "o".
15+
</pre>
16+
17+
#### Example 2:
18+
<pre>
19+
<strong>Input:</strong> s = "ha22", k = 5
20+
<strong>Output:</strong> "h"
21+
<strong>Explanation:</strong> The decoded string is "hahahaha".
22+
The 5th letter is "h".
23+
</pre>
24+
25+
#### Example 3:
26+
<pre>
27+
<strong>Input:</strong> s = "a2345678999999999999999", k = 1
28+
<strong>Output:</strong> "a"
29+
<strong>Explanation:</strong> The decoded string is "a" repeated 8301530446056247680 times.
30+
The 1st letter is "a".
31+
</pre>
32+
33+
#### Constraints:
34+
* `2 <= s.length <= 100`
35+
* `s` consists of lowercase English letters and digits `2` through `9`.
36+
* `s` starts with a letter.
37+
* <code>1 <= k <= 10<sup>9</sup></code>
38+
* It is guaranteed that `k` is less than or equal to the length of the decoded string.
39+
* The decoded string is guaranteed to have less than <code>2<sup>63</sup></code> letters.
40+
41+
## Solutions (Rust)
42+
43+
### 1. Solution
44+
```Rust
45+
impl Solution {
46+
pub fn decode_at_index(s: String, k: i32) -> String {
47+
let mut k = k as i64 - 1;
48+
let mut chars = vec![];
49+
let mut length = 0;
50+
51+
for ch in s.bytes() {
52+
chars.push((ch, length));
53+
54+
if ch.is_ascii_lowercase() {
55+
length += 1;
56+
} else {
57+
length *= (ch - b'0') as i64;
58+
}
59+
60+
if length > k {
61+
break;
62+
}
63+
}
64+
65+
while let Some((ch, i)) = chars.pop() {
66+
if ch.is_ascii_lowercase() {
67+
if i == k {
68+
return String::from_utf8(vec![ch]).unwrap();
69+
}
70+
71+
length -= 1;
72+
} else {
73+
length /= (ch - b'0') as i64;
74+
k %= length;
75+
}
76+
}
77+
78+
unreachable!()
79+
}
80+
}
81+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 880. 索引处的解码字符串
2+
给定一个编码字符串 `S`。请你找出 **解码字符串** 并将其写入磁带。解码时,从编码字符串中 **每次读取一个字符** ,并采取以下步骤:
3+
4+
* 如果所读的字符是字母,则将该字母写在磁带上。
5+
* 如果所读的字符是数字(例如 `d`),则整个当前磁带总共会被重复写 `d-1` 次。
6+
7+
现在,对于给定的编码字符串 `S` 和索引 `K`,查找并返回解码字符串中的第 `K` 个字母。
8+
9+
#### 示例 1:
10+
<pre>
11+
<strong>输入:</strong> s = "leet2code3", k = 10
12+
<strong>输出:</strong> "o"
13+
<strong>解释:</strong> 解码后的字符串为 "leetleetcodeleetleetcodeleetleetcode"。
14+
字符串中的第 10 个字母是 "o"。
15+
</pre>
16+
17+
#### 示例 2:
18+
<pre>
19+
<strong>输入:</strong> s = "ha22", k = 5
20+
<strong>输出:</strong> "h"
21+
<strong>解释:</strong> 解码后的字符串为 "hahahaha"。第 5 个字母是 "h"。
22+
</pre>
23+
24+
#### 示例 3:
25+
<pre>
26+
<strong>输入:</strong> s = "a2345678999999999999999", k = 1
27+
<strong>输出:</strong> "a"
28+
<strong>解释:</strong> 解码后的字符串为 "a" 重复 8301530446056247680 次。第 1 个字母是 "a"。
29+
</pre>
30+
31+
#### 提示:
32+
* `2 <= S.length <= 100`
33+
* `S` 只包含小写字母与数字 `2``9`
34+
* `S` 以字母开头。
35+
* <code>1 <= k <= 10<sup>9</sup></code>
36+
* 题目保证 `K` 小于或等于解码字符串的长度。
37+
* 解码后的字符串保证少于 <code>2<sup>63</sup></code> 个字母。
38+
39+
## 题解 (Rust)
40+
41+
### 1. 题解
42+
```Rust
43+
impl Solution {
44+
pub fn decode_at_index(s: String, k: i32) -> String {
45+
let mut k = k as i64 - 1;
46+
let mut chars = vec![];
47+
let mut length = 0;
48+
49+
for ch in s.bytes() {
50+
chars.push((ch, length));
51+
52+
if ch.is_ascii_lowercase() {
53+
length += 1;
54+
} else {
55+
length *= (ch - b'0') as i64;
56+
}
57+
58+
if length > k {
59+
break;
60+
}
61+
}
62+
63+
while let Some((ch, i)) = chars.pop() {
64+
if ch.is_ascii_lowercase() {
65+
if i == k {
66+
return String::from_utf8(vec![ch]).unwrap();
67+
}
68+
69+
length -= 1;
70+
} else {
71+
length /= (ch - b'0') as i64;
72+
k %= length;
73+
}
74+
}
75+
76+
unreachable!()
77+
}
78+
}
79+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
impl Solution {
2+
pub fn decode_at_index(s: String, k: i32) -> String {
3+
let mut k = k as i64 - 1;
4+
let mut chars = vec![];
5+
let mut length = 0;
6+
7+
for ch in s.bytes() {
8+
chars.push((ch, length));
9+
10+
if ch.is_ascii_lowercase() {
11+
length += 1;
12+
} else {
13+
length *= (ch - b'0') as i64;
14+
}
15+
16+
if length > k {
17+
break;
18+
}
19+
}
20+
21+
while let Some((ch, i)) = chars.pop() {
22+
if ch.is_ascii_lowercase() {
23+
if i == k {
24+
return String::from_utf8(vec![ch]).unwrap();
25+
}
26+
27+
length -= 1;
28+
} else {
29+
length /= (ch - b'0') as i64;
30+
k %= length;
31+
}
32+
}
33+
34+
unreachable!()
35+
}
36+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@
478478
[876][876l] |[Middle of the Linked List][876] |![py]
479479
[877][877l] |[Stone Game][877] |![rs]
480480
[878][878l] |[Nth Magical Number][878] |![py]
481+
[880][880l] |[Decoded String at Index][880] |![rs]
481482
[881][881l] |[Boats to Save People][881] |![rb]
482483
[883][883l] |[Projection Area of 3D Shapes][883] |![rs]
483484
[884][884l] |[Uncommon Words from Two Sentences][884] |![py]
@@ -1747,6 +1748,7 @@
17471748
[876]:Problemset/0876-Middle%20of%20the%20Linked%20List/README.md#876-middle-of-the-linked-list
17481749
[877]:Problemset/0877-Stone%20Game/README.md#877-stone-game
17491750
[878]:Problemset/0878-Nth%20Magical%20Number/README.md#878-nth-magical-number
1751+
[880]:Problemset/0880-Decoded%20String%20at%20Index/README.md#880-decoded-string-at-index
17501752
[881]:Problemset/0881-Boats%20to%20Save%20People/README.md#881-boats-to-save-people
17511753
[883]:Problemset/0883-Projection%20Area%20of%203D%20Shapes/README.md#883-projection-area-of-3d-shapes
17521754
[884]:Problemset/0884-Uncommon%20Words%20from%20Two%20Sentences/README.md#884-uncommon-words-from-two-sentences
@@ -3019,6 +3021,7 @@
30193021
[876l]:https://leetcode.com/problems/middle-of-the-linked-list/
30203022
[877l]:https://leetcode.com/problems/stone-game/
30213023
[878l]:https://leetcode.com/problems/nth-magical-number/
3024+
[880l]:https://leetcode.com/problems/decoded-string-at-index/
30223025
[881l]:https://leetcode.com/problems/boats-to-save-people/
30233026
[883l]:https://leetcode.com/problems/projection-area-of-3d-shapes/
30243027
[884l]:https://leetcode.com/problems/uncommon-words-from-two-sentences/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@
478478
[876][876l] |[链表的中间结点][876] |![py]
479479
[877][877l] |[石子游戏][877] |![rs]
480480
[878][878l] |[第 N 个神奇数字][878] |![py]
481+
[880][880l] |[索引处的解码字符串][880] |![rs]
481482
[881][881l] |[救生艇][881] |![rb]
482483
[883][883l] |[三维形体投影面积][883] |![rs]
483484
[884][884l] |[两句话中的不常见单词][884] |![py]
@@ -1747,6 +1748,7 @@
17471748
[876]:Problemset/0876-Middle%20of%20the%20Linked%20List/README_CN.md#876-链表的中间结点
17481749
[877]:Problemset/0877-Stone%20Game/README_CN.md#877-石子游戏
17491750
[878]:Problemset/0878-Nth%20Magical%20Number/README_CN.md#878-第-n-个神奇数字
1751+
[880]:Problemset/0880-Decoded%20String%20at%20Index/README_CN.md#880-索引处的解码字符串
17501752
[881]:Problemset/0881-Boats%20to%20Save%20People/README_CN.md#881-救生艇
17511753
[883]:Problemset/0883-Projection%20Area%20of%203D%20Shapes/README_CN.md#883-三维形体投影面积
17521754
[884]:Problemset/0884-Uncommon%20Words%20from%20Two%20Sentences/README_CN.md#884-两句话中的不常见单词
@@ -3019,6 +3021,7 @@
30193021
[876l]:https://leetcode.cn/problems/middle-of-the-linked-list/
30203022
[877l]:https://leetcode.cn/problems/stone-game/
30213023
[878l]:https://leetcode.cn/problems/nth-magical-number/
3024+
[880l]:https://leetcode.cn/problems/decoded-string-at-index/
30223025
[881l]:https://leetcode.cn/problems/boats-to-save-people/
30233026
[883l]:https://leetcode.cn/problems/projection-area-of-3d-shapes/
30243027
[884l]:https://leetcode.cn/problems/uncommon-words-from-two-sentences/

0 commit comments

Comments
 (0)