File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * [0761] special-binary-string
3+ */
4+
5+ struct Solution ;
6+
7+ impl Solution {
8+ pub fn make_largest_special ( s : String ) -> String {
9+ let s = s. as_bytes ( ) ;
10+ let mut l = Vec :: new ( ) ;
11+ let mut start = 0 ;
12+ let mut count = 0 ;
13+ for i in 0 ..s. len ( ) {
14+ count += if s[ i] as char == '1' { 1 } else { -1 } ;
15+ if count == 0 {
16+ let substr = std:: str:: from_utf8 ( & s[ start + 1 ..i] ) . unwrap ( ) . to_string ( ) ;
17+ l. push ( "1" . to_string ( ) + & Self :: make_largest_special ( substr) + "0" ) ;
18+ start = i + 1 ;
19+ }
20+ }
21+ l. sort ( ) ;
22+ l. reverse ( ) ;
23+ l. concat ( )
24+ }
25+ }
26+
27+ #[ cfg( test) ]
28+ mod tests {
29+ use super :: * ;
30+
31+ #[ test]
32+ fn test_case0 ( ) {
33+ assert_eq ! (
34+ Solution :: make_largest_special( "11011000" . to_owned( ) ) ,
35+ "11100100" . to_owned( )
36+ ) ;
37+ }
38+ }
Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ mod a0643_maximum_average_subarray_i;
3434mod a0646_maximum_length_of_pair_chain;
3535mod a0654_maximum_binary_tree;
3636mod a0701_insert_into_a_binary_search_tree;
37+ mod a0761_special_binary_string;
3738mod a0867_transpose_matrix;
3839mod a0883_projection_area_of_3d_shapes;
3940mod a0894_all_possible_full_binary_trees;
You can’t perform that action at this time.
0 commit comments