CLOSE
🛠️ Settings

Problem Statement

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.

Examples

Example 1:

Input: s = "hello"
Output: 13

Explanation:

The ASCII values of the characters in s are: 'h' = 104, 'e' = 101, 'l' = 108, 'o' = 111. So, the score of s would be |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13.
Example 2:

Input: s = "zaz"
Output: 50

Explanation:

The ASCII values of the characters in s are: 'z' = 122, 'a' = 97. So, the score of s would be |122 - 97| + |97 - 122| = 25 + 25 = 50.

Constraints:

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

Approach

1️⃣ Linear Iteration

Intuition

To solve the problem, we must calculate the sum of the absolute differences between the ASCII values of all adjacent characters in the input string s.

The absolute difference between two numbers is the positive value of the difference between those numbers, regardless of which one is larger. For example, the absolute difference between 33 and 88 is ∣3−8∣ = ∣−5∣ = ∣3−8∣ = ∣−5∣ = 5.

To solve the given problem, we'll iterate through the string s from the beginning. For each character at index i, we compute the difference between the ASCII values of the character at index i and the character at index i + 1. We then add the absolute value of this difference to a cumulative sum.
This iteration stops at the second-last character because each comparison involves the next character in the string.

Handling character data in different programming languages:

  • In C++ and Java, characters are treated as integer values based on their ASCII or Unicode representations. This allows for direct arithmetic operations such as subtraction between characters.

Algorithm:

  1. Initialize a variable score to 0 to store the cumulative sum.
  2. Iterate over all indices from 0 to length - 1 of the input string. For each index, calculate the absolute difference between the ASCII values of the character at the current index and the character at the next index. Add this difference to the score.
  3. Return the score after the loop completes.

Code:

class Solution {
public:
    int scoreOfString(string s) {
        int score = 0;
        // Iterate over all indices from 0 to the second-to-last index
        // Calculate and accumulate the absolute difference of ASCII values
        // between adjacent characters
        for (int i = 0; i < s.size() - 1; ++i) {
            score += abs(s[i] - s[i + 1]);
        }
        return score;
    }
};

Complexity Analysis:

  • Time Complexity: O(n), where n is the length of the input string.
    • The process involves iterating through the string once, from the first character to the second-last character, making it a linear iteration over n−1 indices
    • At each index, calculating the absolute difference between the ASCII values of two adjacent characters requires constant time.
    • Hence, the total time complexity for this operation is O(n - 1) ~ O(n).
  • Space Complexity: O(1)
    • We only used a single additional variable, score, to accumulate the result. Therefore, the space complexity is O(1), indicating that no additional space proportional to the input size is required.