Score from Adjacent ASCII Differences

Quick Overview

Compute the sum of absolute ASCII differences between adjacent lowercase characters, preserving order and including zero differences.

Score from Adjacent ASCII Differences

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

# Score from Adjacent ASCII Differences Given a string `s`, define its score as the sum of the absolute differences between the ASCII values of every pair of adjacent characters. Return that score. Each adjacent pair contributes once, and the last character is not paired with the first. ### Examples **Example 1** ```text Input: s = "azba" Output: 50 ``` The ASCII values are `97,122,98,97`. The adjacent contributions are `25,24,1`, giving a total of 50. **Example 2** ```text Input: s = "cddf" Output: 3 ``` The ASCII values are `99,100,100,102`. The contributions are `1,0,2`, giving a total of three. Equal neighboring characters contribute zero. ### Constraints - `2 <= s.length <= 100`. - `s` contains only lowercase English letters. ### Clarifying Questions - Are all pairs of characters compared? No; only neighbors in the original string. - Does a decrease in ASCII value contribute a negative number? No; take the absolute difference for every pair. - Is the string sorted first? No; use its given order. - Is there a contribution between the last and first characters? No. ### C++ Interface ```cpp class Solution { public: int scoreOfString(string s) { } }; ``` ### Reasoning Checkpoints - Apply the exact adjacent-pair score definition without omitting or duplicating a contribution. - Handle equal characters and increases or decreases in ASCII value. - Justify the result type and resource costs under the stated bounds. ```hint Sum the consecutive differences For each position after the first, convert its character and the preceding character to integers before subtracting. Add the nonnegative magnitude of that difference to the score. ```

Overview: Compute the sum of absolute ASCII differences between adjacent lowercase characters, preserving order and including zero differences.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Score from Adjacent ASCII Differences

Given a string s, define its score as the sum of the absolute differences between the ASCII values of every pair of adjacent characters.

Return that score. Each adjacent pair contributes once, and the last character is not paired with the first.

Examples

Example 1

Input: s = "azba"
Output: 50

The ASCII values are 97,122,98,97. The adjacent contributions are 25,24,1, giving a total of 50.

Example 2

Input: s = "cddf"
Output: 3

The ASCII values are 99,100,100,102. The contributions are 1,0,2, giving a total of three. Equal neighboring characters contribute zero.

Constraints

  • 2 <= s.length <= 100 .
  • s contains only lowercase English letters.

Clarifying Questions Guidance

  • Are all pairs of characters compared? No; only neighbors in the original string.
  • Does a decrease in ASCII value contribute a negative number? No; take the absolute difference for every pair.
  • Is the string sorted first? No; use its given order.
  • Is there a contribution between the last and first characters? No.

C++ Interface

class Solution {
public:
    int scoreOfString(string s) {
        
    }
};

Reasoning Checkpoints

  • Apply the exact adjacent-pair score definition without omitting or duplicating a contribution.
  • Handle equal characters and increases or decreases in ASCII value.
  • Justify the result type and resource costs under the stated bounds.