diff --git a/src/problem/mod.rs b/src/problem/mod.rs index fb72952..450b1e2 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -538,3 +538,5 @@ mod p3305_count_of_substrings_containing_every_vowel_and_k_consonants_i; mod p3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii; mod p3340_check_balanced_string; + +mod p3110_score_of_a_string; diff --git a/src/problem/p3110_score_of_a_string.rs b/src/problem/p3110_score_of_a_string.rs new file mode 100644 index 0000000..4bf6921 --- /dev/null +++ b/src/problem/p3110_score_of_a_string.rs @@ -0,0 +1,28 @@ +/** + * [3110] Score of a String + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn score_of_string(s: String) -> i32 { + s.bytes() + .zip(s.bytes().skip(1)) + .map(|(first, second)| first.abs_diff(second) as i32) + .sum::() + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3110() { + assert_eq!(13, Solution::score_of_string("hello".to_owned())); + assert_eq!(50, Solution::score_of_string("zaz".to_owned())); + } +}