-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathremoving-stars-from-a-string.rs
99 lines (95 loc) · 3.21 KB
/
removing-stars-from-a-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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// 2390. Removing Stars From a String
// 🟠 Medium
//
// https://leetcode.com/problems/removing-stars-from-a-string/
//
// Tags: String - Stack - Simulation
struct Solution;
impl Solution {
/// Use a stack to build the result, iterate over the characters in the
/// input, when we see a '*', pop the last character from the stack, any
/// other character, we push into the stack. Since Strings in Rust are
/// mutable, we can use a String as our stack and return that directly.
///
/// Time complexity: O(n) - We visit all characters in the input string and
/// do O(1) work for each.
/// Space complexity: O(n) - The stack grows in size linearly with the input.
///
/// Runtime 3 ms Beats 100%
/// Memory 2.3 MB Beats 100%
pub fn remove_stars(s: String) -> String {
let mut res = String::with_capacity(s.len());
for c in s.chars() {
if c == '*' {
res.pop();
} else {
res.push(c);
}
}
res
}
/// Use an iterator and the fold operator to pop characters from the result
/// that we are building.
///
/// Time complexity: O(n) - We visit all characters in the input string and
/// do O(1) work for each.
/// Space complexity: O(n) - The stack grows in size linearly with the input.
///
/// Runtime 7 ms Beats 100%
/// Memory 2.3 MB Beats 100%
pub fn remove_stars_2(s: String) -> String {
s.chars().fold(String::new(), |mut res, c| {
match c {
'*' => {
res.pop();
}
_ => res.push(c),
};
res
})
}
/// Iterate in reverse over the characters in the input keeping count of how
/// many skips we are due, when we see a '*' we increase the count of skips,
/// when we see a character, if the count of skips is greater than 0, we
/// decrease, if it is 0, we push the character to the result
///
/// Time complexity: O(n) - We visit all characters in the input string and
/// do O(1) work for each.
/// Space complexity: O(n) - The stack grows in size linearly with the input.
///
/// Runtime 6 ms Beats 100%
/// Memory 2.4 MB Beats 100%
pub fn remove_stars_3(s: String) -> String {
let mut res = String::with_capacity(s.len());
// Needs to be at least 32 bit for the 10^5 max value.
let mut skip = 0;
for c in s.chars().rev() {
if c == '*' {
skip += 1;
} else {
if skip > 0 {
skip -= 1;
} else {
res.push(c);
}
}
}
res.chars().rev().collect()
}
}
// Tests.
fn main() {
let tests = [("leet**cod*e", "lecoe"), ("erase*****", "")];
for t in tests {
assert_eq!(Solution::remove_stars(String::from(t.0)), String::from(t.1));
assert_eq!(
Solution::remove_stars_2(String::from(t.0)),
String::from(t.1)
);
assert_eq!(
Solution::remove_stars_3(String::from(t.0)),
String::from(t.1)
);
}
println!("\x1b[92m» All tests passed!\x1b[0m")
}