Skip to content

Commit 60924ec

Browse files
committed
Add problem 3170: Lexicographically Minimum String After Removing Stars
1 parent c0ab54f commit 60924ec

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,6 +2119,7 @@ pub mod problem_3163_string_compression_iii;
21192119
pub mod problem_3164_find_the_number_of_good_pairs_ii;
21202120
pub mod problem_3168_minimum_number_of_chairs_in_a_waiting_room;
21212121
pub mod problem_3169_count_days_without_meetings;
2122+
pub mod problem_3170_lexicographically_minimum_string_after_removing_stars;
21222123
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21232124

21242125
#[cfg(test)]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn clear_stars(s: String) -> String {
7+
let mut indices = [const { Vec::new() }; 26];
8+
let mut s = s.into_bytes();
9+
10+
for i in 0..s.len() {
11+
let c = s[i];
12+
13+
if let Some(indices) = indices.get_mut(usize::from(c).wrapping_sub(usize::from(b'a'))) {
14+
indices.push(i as u32);
15+
} else {
16+
let index = indices.iter_mut().find_map(Vec::pop).unwrap();
17+
18+
s[index as usize] = b'*';
19+
}
20+
}
21+
22+
s.retain(|&c| c != b'*');
23+
24+
String::from_utf8(s).unwrap()
25+
}
26+
}
27+
28+
// ------------------------------------------------------ snip ------------------------------------------------------ //
29+
30+
impl super::Solution for Solution {
31+
fn clear_stars(s: String) -> String {
32+
Self::clear_stars(s)
33+
}
34+
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
#[test]
39+
fn test_solution() {
40+
super::super::tests::run::<super::Solution>();
41+
}
42+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod greedy;
2+
3+
pub trait Solution {
4+
fn clear_stars(s: String) -> String;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [("aaba*", "aab"), ("abc", "abc")];
13+
14+
for (s, expected) in test_cases {
15+
assert_eq!(S::clear_stars(s.to_string()), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)