feat: add initial implementation of score_of_a_string with main function
This commit is contained in:
parent
2026979f5e
commit
c41b628fab
7
3110_score_of_a_string/Cargo.lock
generated
Normal file
7
3110_score_of_a_string/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "score_of_a_string"
|
||||||
|
version = "0.1.0"
|
6
3110_score_of_a_string/Cargo.toml
Normal file
6
3110_score_of_a_string/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "score_of_a_string"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
14
3110_score_of_a_string/src/main.rs
Normal file
14
3110_score_of_a_string/src/main.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
fn main() {
|
||||||
|
println!("{}", Solution::score_of_string("hello".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Solution;
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn score_of_string(s: String) -> i32 {
|
||||||
|
s.chars()
|
||||||
|
.zip(s.chars().skip(1))
|
||||||
|
.map(|(a, b)| (a as i32 - b as i32).abs())
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user