Skip to main content
Back to problems
#3110
Easy Algorithms

Score of a string

String
91.4% acceptance
Feb 23, 2026
838
52
You are given a string s. The score of a string is defined as the sum of the absolute difference between the ASCII values of adjacent characters. Return the score of s.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn score_of_string(s: String) -> i32 {
    s.as_bytes()
      .windows(2)
      .map(|w| (w[0] as i32 - w[1] as i32).abs())
      .sum()
  }
}