Skip to content

Commit de264fd

Browse files
committed
+ problem 466
1 parent b2b31da commit de264fd

File tree

5 files changed

+184
-0
lines changed

5 files changed

+184
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# 466. Count The Repetitions
2+
We define `str = [s, n]` as the string `str` which consists of the string `s` concatenated `n` times.
3+
4+
* For example, `str == ["abc", 3] =="abcabcabc"`.
5+
6+
We define that string `s1` can be obtained from string `s2` if we can remove some characters from `s2` such that it becomes `s1`.
7+
8+
* For example, `s1 = "abc"` can be obtained from `s2 = "abdbec"` based on our definition by removing the bolded underlined characters.
9+
10+
You are given two strings `s1` and `s2` and two integers `n1` and `n2`. You have the two strings `str1 = [s1, n1]` and `str2 = [s2, n2]`.
11+
12+
Return *the maximum integer* `m` *such that* `str = [str2, m]` *can be obtained from* `str1`.
13+
14+
#### Example 1:
15+
<pre>
16+
<strong>Input:</strong> s1 = "acb", n1 = 4, s2 = "ab", n2 = 2
17+
<strong>Output:</strong> 2
18+
</pre>
19+
20+
#### Example 2:
21+
<pre>
22+
<strong>Input:</strong> s1 = "acb", n1 = 1, s2 = "acb", n2 = 1
23+
<strong>Output:</strong> 1
24+
</pre>
25+
26+
#### Constraints:
27+
* `1 <= s1.length, s2.length <= 100`
28+
* `s1` and `s2` consist of lowercase English letters.
29+
* <code>1 <= n1, n2 <= 10<sup>6</sup></code>
30+
31+
## Solutions (Rust)
32+
33+
### 1. Solution
34+
```Rust
35+
use std::collections::HashMap;
36+
37+
impl Solution {
38+
pub fn get_max_repetitions(s1: String, n1: i32, s2: String, n2: i32) -> i32 {
39+
let s1 = s1.as_bytes();
40+
let s2 = s2.as_bytes();
41+
let mut i = 0;
42+
let mut memo = HashMap::new();
43+
let mut count2 = 0;
44+
45+
for _ in 0..n1 {
46+
if let Some(&(count, j)) = memo.get(&i) {
47+
count2 += count;
48+
i = j;
49+
continue;
50+
}
51+
52+
let init = i;
53+
let mut count = 0;
54+
55+
for j in 0..s1.len() {
56+
if s1[j] == s2[i] {
57+
i = (i + 1) % s2.len();
58+
if i == 0 {
59+
count += 1;
60+
}
61+
}
62+
}
63+
64+
memo.insert(init, (count, i));
65+
count2 += count;
66+
}
67+
68+
count2 / n2
69+
}
70+
}
71+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# 466. 统计重复个数
2+
定义 `str = [s, n]` 表示 `str``n` 个字符串 `s` 连接构成。
3+
4+
* 例如,`str == ["abc", 3] =="abcabcabc"`
5+
6+
如果可以从 `s2` 中删除某些字符使其变为 `s1`,则称字符串 `s1` 可以从字符串 `s2` 获得。
7+
8+
* 例如,根据定义,`s1 = "abc"` 可以从 `s2 = "abdbec"` 获得,仅需要删除加粗且用斜体标识的字符。
9+
10+
现在给你两个字符串 `s1``s2` 和两个整数 `n1``n2` 。由此构造得到两个字符串,其中 `str1 = [s1, n1]``str2 = [s2, n2]`
11+
12+
请你找出一个最大整数 `m` ,以满足 `str = [str2, m]` 可以从 `str1` 获得。
13+
14+
#### 示例 1:
15+
<pre>
16+
<strong>输入:</strong> s1 = "acb", n1 = 4, s2 = "ab", n2 = 2
17+
<strong>输出:</strong> 2
18+
</pre>
19+
20+
#### 示例 2:
21+
<pre>
22+
<strong>输入:</strong> s1 = "acb", n1 = 1, s2 = "acb", n2 = 1
23+
<strong>输出:</strong> 1
24+
</pre>
25+
26+
#### 提示:
27+
* `1 <= s1.length, s2.length <= 100`
28+
* `s1``s2` 由小写英文字母组成
29+
* <code>1 <= n1, n2 <= 10<sup>6</sup></code>
30+
31+
## 题解 (Rust)
32+
33+
### 1. 题解
34+
```Rust
35+
use std::collections::HashMap;
36+
37+
impl Solution {
38+
pub fn get_max_repetitions(s1: String, n1: i32, s2: String, n2: i32) -> i32 {
39+
let s1 = s1.as_bytes();
40+
let s2 = s2.as_bytes();
41+
let mut i = 0;
42+
let mut memo = HashMap::new();
43+
let mut count2 = 0;
44+
45+
for _ in 0..n1 {
46+
if let Some(&(count, j)) = memo.get(&i) {
47+
count2 += count;
48+
i = j;
49+
continue;
50+
}
51+
52+
let init = i;
53+
let mut count = 0;
54+
55+
for j in 0..s1.len() {
56+
if s1[j] == s2[i] {
57+
i = (i + 1) % s2.len();
58+
if i == 0 {
59+
count += 1;
60+
}
61+
}
62+
}
63+
64+
memo.insert(init, (count, i));
65+
count2 += count;
66+
}
67+
68+
count2 / n2
69+
}
70+
}
71+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn get_max_repetitions(s1: String, n1: i32, s2: String, n2: i32) -> i32 {
5+
let s1 = s1.as_bytes();
6+
let s2 = s2.as_bytes();
7+
let mut i = 0;
8+
let mut memo = HashMap::new();
9+
let mut count2 = 0;
10+
11+
for _ in 0..n1 {
12+
if let Some(&(count, j)) = memo.get(&i) {
13+
count2 += count;
14+
i = j;
15+
continue;
16+
}
17+
18+
let init = i;
19+
let mut count = 0;
20+
21+
for j in 0..s1.len() {
22+
if s1[j] == s2[i] {
23+
i = (i + 1) % s2.len();
24+
if i == 0 {
25+
count += 1;
26+
}
27+
}
28+
}
29+
30+
memo.insert(init, (count, i));
31+
count2 += count;
32+
}
33+
34+
count2 / n2
35+
}
36+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@
355355
[462][462l] |[Minimum Moves to Equal Array Elements II][462] |![rs]
356356
[463][463l] |[Island Perimeter][463] |![rs]
357357
[464][464l] |[Can I Win][464] |![py]
358+
[466][466l] |[Count The Repetitions][466] |![rs]
358359
[467][467l] |[Unique Substrings in Wraparound String][467] |![rb]&nbsp;&nbsp;![rs]
359360
[468][468l] |[Validate IP Address][468] |![rs]
360361
[470][470l] |[Implement Rand10() Using Rand7()][470] |![rs]
@@ -2297,6 +2298,7 @@
22972298
[462]:Problemset/0462-Minimum%20Moves%20to%20Equal%20Array%20Elements%20II/README.md#462-minimum-moves-to-equal-array-elements-ii
22982299
[463]:Problemset/0463-Island%20Perimeter/README.md#463-island-perimeter
22992300
[464]:Problemset/0464-Can%20I%20Win/README.md#464-can-i-win
2301+
[466]:Problemset/0466-Count%20The%20Repetitions/README.md#466-count-the-repetitions
23002302
[467]:Problemset/0467-Unique%20Substrings%20in%20Wraparound%20String/README.md#467-unique-substrings-in-wraparound-string
23012303
[468]:Problemset/0468-Validate%20IP%20Address/README.md#468-validate-ip-address
23022304
[470]:Problemset/0470-Implement%20Rand10\(\)%20Using%20Rand7\(\)/README.md#470-implement-rand10-using-rand7
@@ -4233,6 +4235,7 @@
42334235
[462l]:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/
42344236
[463l]:https://leetcode.com/problems/island-perimeter/
42354237
[464l]:https://leetcode.com/problems/can-i-win/
4238+
[466l]:https://leetcode.com/problems/count-the-repetitions/
42364239
[467l]:https://leetcode.com/problems/unique-substrings-in-wraparound-string/
42374240
[468l]:https://leetcode.com/problems/validate-ip-address/
42384241
[470l]:https://leetcode.com/problems/implement-rand10-using-rand7/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@
355355
[462][462l] |[最少移动次数使数组元素相等 II][462] |![rs]
356356
[463][463l] |[岛屿的周长][463] |![rs]
357357
[464][464l] |[我能赢吗][464] |![py]
358+
[466][466l] |[统计重复个数][466] |![rs]
358359
[467][467l] |[环绕字符串中唯一的子字符串][467] |![rb]&nbsp;&nbsp;![rs]
359360
[468][468l] |[验证IP地址][468] |![rs]
360361
[470][470l] |[用 Rand7() 实现 Rand10()][470] |![rs]
@@ -2297,6 +2298,7 @@
22972298
[462]:Problemset/0462-Minimum%20Moves%20to%20Equal%20Array%20Elements%20II/README_CN.md#462-最少移动次数使数组元素相等-ii
22982299
[463]:Problemset/0463-Island%20Perimeter/README_CN.md#463-岛屿的周长
22992300
[464]:Problemset/0464-Can%20I%20Win/README_CN.md#464-我能赢吗
2301+
[466]:Problemset/0466-Count%20The%20Repetitions/README_CN.md#466-统计重复个数
23002302
[467]:Problemset/0467-Unique%20Substrings%20in%20Wraparound%20String/README_CN.md#467-环绕字符串中唯一的子字符串
23012303
[468]:Problemset/0468-Validate%20IP%20Address/README_CN.md#468-验证ip地址
23022304
[470]:Problemset/0470-Implement%20Rand10\(\)%20Using%20Rand7\(\)/README_CN.md#470-用-Rand7-实现-Rand10
@@ -4233,6 +4235,7 @@
42334235
[462l]:https://leetcode.cn/problems/minimum-moves-to-equal-array-elements-ii/
42344236
[463l]:https://leetcode.cn/problems/island-perimeter/
42354237
[464l]:https://leetcode.cn/problems/can-i-win/
4238+
[466l]:https://leetcode.cn/problems/count-the-repetitions/
42364239
[467l]:https://leetcode.cn/problems/unique-substrings-in-wraparound-string/
42374240
[468l]:https://leetcode.cn/problems/validate-ip-address/
42384241
[470l]:https://leetcode.cn/problems/implement-rand10-using-rand7/

0 commit comments

Comments
 (0)