String 3110. Score of a String ¶ Time: $O(n)$ Space: $O(1)$ C++JavaPython 1 2 3 4 5 6 7 8 9 10 11class Solution { public: int scoreOfString(string s) { int ans = 0; for (int i = 1; i < s.length(); ++i) ans += abs(s[i] - s[i - 1]); return ans; } }; 1 2 3 4 5 6 7 8 9 10class Solution { public int scoreOfString(String s) { int ans = 0; for (int i = 1; i < s.length(); ++i) ans += Math.abs(s.charAt(i) - s.charAt(i - 1)); return ans; } } 1 2 3 4class Solution: def scoreOfString(self, s: str) -> int: return sum(abs(ord(a) - ord(b)) for a, b in itertools.pairwise(s))