Skip to content

Commit 7355f18

Browse files
authored
feat: add rust solution to lc problem: No.2696 (#1076)
1 parent 1a14164 commit 7355f18

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

solution/2600-2699/2696.Minimum String Length After Removing Substrings/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,32 @@ function minLength(s: string): number {
149149
}
150150
```
151151

152+
### **Rust**
153+
154+
```rust
155+
impl Solution {
156+
pub fn min_length(s: String) -> i32 {
157+
let mut ans: Vec<u8> = Vec::new();
158+
159+
for c in s.bytes() {
160+
if let Some(last) = ans.last() {
161+
if *last == b'A' && c == b'B' {
162+
ans.pop();
163+
} else if *last == b'C' && c == b'D' {
164+
ans.pop();
165+
} else {
166+
ans.push(c);
167+
}
168+
} else {
169+
ans.push(c);
170+
}
171+
}
172+
173+
ans.len() as i32
174+
}
175+
}
176+
```
177+
152178
### **...**
153179

154180
```

solution/2600-2699/2696.Minimum String Length After Removing Substrings/README_EN.md

+26
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,32 @@ function minLength(s: string): number {
131131
}
132132
```
133133

134+
### **Rust**
135+
136+
```rust
137+
impl Solution {
138+
pub fn min_length(s: String) -> i32 {
139+
let mut ans: Vec<u8> = Vec::new();
140+
141+
for c in s.bytes() {
142+
if let Some(last) = ans.last() {
143+
if *last == b'A' && c == b'B' {
144+
ans.pop();
145+
} else if *last == b'C' && c == b'D' {
146+
ans.pop();
147+
} else {
148+
ans.push(c);
149+
}
150+
} else {
151+
ans.push(c);
152+
}
153+
}
154+
155+
ans.len() as i32
156+
}
157+
}
158+
```
159+
134160
### **...**
135161

136162
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn min_length(s: String) -> i32 {
3+
let mut ans: Vec<u8> = Vec::new();
4+
5+
for c in s.bytes() {
6+
if let Some(last) = ans.last() {
7+
if *last == b'A' && c == b'B' {
8+
ans.pop();
9+
} else if *last == b'C' && c == b'D' {
10+
ans.pop();
11+
} else {
12+
ans.push(c);
13+
}
14+
} else {
15+
ans.push(c);
16+
}
17+
}
18+
19+
ans.len() as i32
20+
}
21+
}

0 commit comments

Comments
 (0)