Skip to content

Commit 418b55a

Browse files
committed
Add problem 3110: Score of a String
1 parent af540a8 commit 418b55a

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,6 +2091,7 @@ pub mod problem_3101_count_alternating_subarrays;
20912091
pub mod problem_3105_longest_strictly_increasing_or_strictly_decreasing_subarray;
20922092
pub mod problem_3106_lexicographically_smallest_string_after_operations_with_constraint;
20932093
pub mod problem_3107_minimum_operations_to_make_median_of_array_equal_to_k;
2094+
pub mod problem_3110_score_of_a_string;
20942095
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
20952096

20962097
#[cfg(test)]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

0 commit comments

Comments
 (0)