|
| 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 | +``` |
0 commit comments