File tree Expand file tree Collapse file tree 3 files changed +47
-0
lines changed
problem_3110_score_of_a_string Expand file tree Collapse file tree 3 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -2091,6 +2091,7 @@ pub mod problem_3101_count_alternating_subarrays;
20912091pub mod problem_3105_longest_strictly_increasing_or_strictly_decreasing_subarray;
20922092pub mod problem_3106_lexicographically_smallest_string_after_operations_with_constraint;
20932093pub mod problem_3107_minimum_operations_to_make_median_of_array_equal_to_k;
2094+ pub mod problem_3110_score_of_a_string;
20942095pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
20952096
20962097#[ 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 score_of_string ( s : String ) -> i32 {
7+ s. as_bytes ( )
8+ . windows ( 2 )
9+ . map ( |window| i32:: from ( window[ 0 ] . abs_diff ( window[ 1 ] ) ) )
10+ . sum ( )
11+ }
12+ }
13+
14+ // ------------------------------------------------------ snip ------------------------------------------------------ //
15+
16+ impl super :: Solution for Solution {
17+ fn score_of_string ( s : String ) -> i32 {
18+ Self :: score_of_string ( s)
19+ }
20+ }
21+
22+ #[ cfg( test) ]
23+ mod tests {
24+ #[ test]
25+ fn test_solution ( ) {
26+ super :: super :: tests:: run :: < super :: Solution > ( ) ;
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ pub mod iterative;
2+
3+ pub trait Solution {
4+ fn score_of_string ( s : String ) -> i32 ;
5+ }
6+
7+ #[ cfg( test) ]
8+ mod tests {
9+ use super :: Solution ;
10+
11+ pub fn run < S : Solution > ( ) {
12+ let test_cases = [ ( "hello" , 13 ) , ( "zaz" , 50 ) ] ;
13+
14+ for ( s, expected) in test_cases {
15+ assert_eq ! ( S :: score_of_string( s. to_string( ) ) , expected) ;
16+ }
17+ }
18+ }
You can’t perform that action at this time.
0 commit comments