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
.
LeetCode Problem - 3110
class Solution {
public int scoreOfString(String s) {
// Initialize the score
int ans = 0;
// Iterate through the string
for (int i = 0; i < s.length() - 1; i++) {
// Get the Unicode code point of the current and next characters
int currentVal = s.codePointAt(i);
int nextVal = s.codePointAt(i + 1);
// Calculate the absolute difference between the Unicode code points and add to the score
ans += Math.abs(currentVal - nextVal);
}
// Return the total score
return ans;
}
}