[Easy] 3110. Score of a String
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 <= 100sconsists only of lowercase English letters.
Thinking Process
- 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.
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 {
public:
int scoreOfString(string s) {
int sum = 0;
for(int i = 0; i < (int)s.length() - 1; i++) {
sum += abs(s[i] - s[i + 1]);
}
return sum;
}
};
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:
- 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
- Minimum length (n=2):
s = "ab"→|97 - 98| = 1 - Same characters:
s = "aa"→|97 - 97| = 0 - Maximum difference:
s = "az"→|97 - 122| = 25 - Repeated characters:
s = "aaa"→0 + 0 = 0 -
Alternating:
s = "abab"→1 + 1 + 1 = 3 - Off-by-one error: Looping to
s.length()instead ofs.length() - 1 - Unsigned comparison: Not casting
s.length()tointcan cause issues - Forgetting absolute value: Using
s[i] - s[i+1]withoutabs() - Empty string: Not handling (though constraints guarantee
n >= 2) - Integer overflow: Not an issue here since ASCII values are small (97-122)
Related Problems
- LC 3111: Minimum Rectangles to Cover Points - Similar string/array processing
- LC 3112: Minimum Time to Visit Disappearing Nodes - Graph traversal
- LC 13: Roman to Integer - Character value processing
- LC 171: Excel Sheet Column Number - Character to number conversion
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
- Simple Simulation: No complex algorithm needed, just iterate and sum
- ASCII Conversion: Characters automatically convert to integers in C++
- Boundary Handling: Loop from
0tolength-2to access all adjacent pairs - Type Safety: Cast
s.length()tointto avoid unsigned comparison issues - Absolute Value: Always use
abs()to ensure positive differences
References
- LC 3110: Score of a String on LeetCode
- LeetCode Discuss — LC 3110: Score of a String
- LeetCode Editorial (may require premium)