1 ms Beats 99.49% | 41.64 MB Beats 98.46%
IntuitionThe problem requires calculating the total "score" of a string based on the absolute difference in ASCII values between each pair of adjacent character
# Intuition
The problem requires calculating the total "score" of a string based on the absolute difference in ASCII values between each pair of adjacent characters.  
My first thought was to iterate through the string and compute the difference in ASCII values between consecutive characters. Since ASCII values are integers, using `Math.abs()` on their difference gives the required score for each adjacent pair.

---

# Approach
1. Convert the input string into a character array for efficient access.
2. Initialize a variable `ans` to store the cumulative score.
3. Loop through the string starting from the second character.
4. For each character, calculate the absolute difference between it and the previous character using `Math.abs(a[i] - a[i-1])`.
5. Add the result to `ans`.
6. Finally, return `ans` as the total score.

---

# Complexity
- **Time complexity:**  
  $$O(n)$$  
  We traverse the string once, where \( n \) is the length of the string.

- **Space complexity:**  
  $$O(n)$$  
  Due to the creation of a character array from the string.

---

# Code
```java
class Solution {
    public int scoreOfString(String s) {
        int ans = 0;
        int n = s.length();
        char[] a = s.toCharArray();
        for (int i = 1; i < n; i++) {
            ans += Math.abs(a[i] - a[i - 1]);
        }
        return ans;
    }
}
```
['Java']