-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathminimum-additions-to-make-valid-string.rs
50 lines (45 loc) · 1.36 KB
/
minimum-additions-to-make-valid-string.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
impl Solution {
pub fn add_minimum(word: String) -> i32 {
let mut result = 0;
let mut index = 0usize;
let word = word.as_bytes();
while index < word.len() {
match word[index] {
b'a' => match (word.get(index + 1), word.get(index + 2)) {
(Some(b'b'), Some(b'c')) => index += 3,
(Some(b'b'), _) => {
index += 2;
result += 1;
}
(Some(b'c'), _) => {
index += 2;
result += 1;
}
_ => {
index += 1;
result += 2;
}
},
b'b' => match word.get(index + 1) {
Some(b'c') => {
index += 2;
result += 1;
}
_ => {
index += 1;
result += 2;
}
},
b'c' => {
index += 1;
result += 2;
}
_ => unreachable!(),
}
}
result
}
}