[LeetCode] Score of a String

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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int scoreOfString(string s) {
int res = 0;
for(int i = 0; i < s.length() - 1; i++) {
res += abs(s[i] - s[i+1]);
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2024/04/13/PS/LeetCode/score-of-a-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.