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, '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, 'z' = 122.
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.

Thinking Process

  1. Simple Simulation: No complex algorithm needed, just iterate and sum
  • Strings often need frequency maps or two-pointer scans.
  • Watch index bounds and empty-string edge cases.
  • Stack helps with nested or repeated patterns.
Array + hash map 2 7 11 map hash map for O(1) lookups

Common Approaches

Typical techniques for this pattern:

Approach Time Space Notes
Two pointers on string (this problem) O(n) O(1) Palindrome, parsing
Hash map / frequency O(n) O(k) Anagram, character counts
KMP / rolling hash O(n) O(n) Pattern matching
Stack parsing O(n) O(n) Decode string, parentheses

Solution

class Solution:
    def scoreOfString(self, s):
        total = 0

        for i in range(len(s) - 1):
            total += abs(ord(s[i]) - ord(s[i + 1]))

        return total

Solution Explanation

Approach: Two pointers on string (this problem)

Key idea: 1. Simple Simulation: No complex algorithm needed, just iterate and sum

How the code works:

  1. Simple Simulation: No complex algorithm needed, just iterate and sum
    • Strings often need frequency maps or two-pointer scans.
    • Watch index bounds and empty-string edge cases.
    • Stack helps with nested or repeated patterns.

Walkthrough — input s = "hello", expected output 13:

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

Common Mistakes

  1. Minimum length (n=2): s = "ab"|97 - 98| = 1
  2. Same characters: s = "aa"|97 - 97| = 0
  3. Maximum difference: s = "az"|97 - 122| = 25
  4. Repeated characters: s = "aaa"0 + 0 = 0
  5. Alternating: s = "abab"1 + 1 + 1 = 3

  6. Off-by-one error: Looping to s.length() instead of s.length() - 1
  7. Unsigned comparison: Not casting s.length() to int can cause issues
  8. Forgetting absolute value: Using s[i] - s[i+1] without abs()
  9. Empty string: Not handling (though constraints guarantee n >= 2)
  10. Integer overflow: Not an issue here since ASCII values are small (97-122)

This problem is a simple simulation exercise that demonstrates basic string iteration and ASCII value manipulation. The key is to iterate through adjacent pairs and sum their absolute differences.

Key Takeaways

  1. Simple Simulation: No complex algorithm needed, just iterate and sum
  2. ASCII Conversion: Characters automatically convert to integers in C++
  3. Boundary Handling: Loop from 0 to length-2 to access all adjacent pairs
  4. Type Safety: Cast s.length() to int to avoid unsigned comparison issues
  5. Absolute Value: Always use abs() to ensure positive differences

References

Template Reference