File tree Expand file tree Collapse file tree 3 files changed +61
-0
lines changed
problem_3170_lexicographically_minimum_string_after_removing_stars Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -2119,6 +2119,7 @@ pub mod problem_3163_string_compression_iii;
21192119pub mod problem_3164_find_the_number_of_good_pairs_ii;
21202120pub mod problem_3168_minimum_number_of_chairs_in_a_waiting_room;
21212121pub mod problem_3169_count_days_without_meetings;
2122+ pub mod problem_3170_lexicographically_minimum_string_after_removing_stars;
21222123pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21232124
21242125#[ cfg( test) ]
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments