Skip to content

Commit 49582fe

Browse files
committed
feat: add rust solution to lc problem: No.3330
No.3330.Find the Original Typed String I
1 parent 6ef2369 commit 49582fe

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

solution/3300-3399/3330.Find the Original Typed String I/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,15 @@ tags:
7575

7676
<!-- solution:start -->
7777

78-
### 方法一
78+
### 方法一:直接遍历
79+
80+
根据题目描述,如果所有相邻字符都不相同,那么只有 1 种可能的输入字符串;如果有 1 对相邻字符相同,例如 "abbc",那么可能的输入字符串有 2 种:"abc" 和 "abbc"。
81+
82+
依此类推,如果有 $k$ 对相邻字符相同,那么可能的输入字符串有 $k + 1$ 种。
83+
84+
因此,我们只需要遍历字符串,统计相邻字符相同的对数再加 1 即可。
85+
86+
时间复杂度 $O(n)$,其中 $n$ 为字符串的长度。空间复杂度 $O(1)$。
7987

8088
<!-- tabs:start -->
8189

@@ -144,6 +152,16 @@ function possibleStringCount(word: string): number {
144152
}
145153
```
146154

155+
#### Rust
156+
157+
```rust
158+
impl Solution {
159+
pub fn possible_string_count(word: String) -> i32 {
160+
1 + word.as_bytes().windows(2).filter(|w| w[0] == w[1]).count() as i32
161+
}
162+
}
163+
```
164+
147165
<!-- tabs:end -->
148166

149167
<!-- solution:end -->

solution/3300-3399/3330.Find the Original Typed String I/README_EN.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,15 @@ tags:
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Direct Traversal
77+
78+
According to the problem description, if all adjacent characters are different, there is only 1 possible original input string. If there is 1 pair of adjacent identical characters, such as "abbc", then there are 2 possible original strings: "abc" and "abbc".
79+
80+
By analogy, if there are $k$ pairs of adjacent identical characters, then there are $k + 1$ possible original input strings.
81+
82+
Therefore, we just need to traverse the string, count the number of pairs of adjacent identical characters, and add 1.
83+
84+
The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.
7785

7886
<!-- tabs:start -->
7987

@@ -142,6 +150,16 @@ function possibleStringCount(word: string): number {
142150
}
143151
```
144152

153+
#### Rust
154+
155+
```rust
156+
impl Solution {
157+
pub fn possible_string_count(word: String) -> i32 {
158+
1 + word.as_bytes().windows(2).filter(|w| w[0] == w[1]).count() as i32
159+
}
160+
}
161+
```
162+
145163
<!-- tabs:end -->
146164

147165
<!-- solution:end -->
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn possible_string_count(word: String) -> i32 {
3+
1 + word.as_bytes().windows(2).filter(|w| w[0] == w[1]).count() as i32
4+
}
5+
}

0 commit comments

Comments
 (0)