Score of a String
Solution
This solution directly calculates the score by iterating through adjacent characters and summing the absolute difference of their ASCII values.
Pseudocode
- Initialize a variable 
scoreto 0. - Iterate through the input string 
sfrom the first character up to the second-to-last character. - In each step, calculate the absolute difference between the ASCII value of the current character and the ASCII value of the next character.
 - Add this difference to the 
score. - After the loop completes, return the final 
score. 
Code
class Solution:
    def scoreOfString(self, s: str) -> int:
        result = 0
        for i in range(len(s) - 1):
            result += abs(ord(s[i]) - ord(s[i + 1]))
        return result
Complexity Analysis
- Time Complexity: 
, where is the length of the string s. We iterate through the string once. - Space Complexity: 
, as we only use a single variable to store the result, requiring constant extra space.  
Tricks
- Adjacent Element Processing: The loop 
for i in range(len(s) - 1)is a classic pattern for iterating over adjacent pairs of elements in an array or string. - Character-to-Integer Conversion: Use the 
ord()function to get the underlying integer (ASCII/Unicode) value of a character, allowing for arithmetic operations.