Leetcode - 3110. Score of a String
Leetcode - 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.
Solution
1
2
3
4
5
6
7
8
9
10
func scoreOfString(s string) int {
sumTotal := 0
for i:=1; i<len(s); i++{
diff := int(math.Abs(float64(s[i]) - float64(s[i-1])))
sumTotal += diff
}
return sumTotal
}
This post is licensed under CC BY 4.0 by the author.