Easy and simple Approach | Beats 99.87%
Intuition\nWhen I first saw this problem, I thought about a simple way to calculate the "score" of a string by comparing pairs of characters. The score calculat
## Intuition\nWhen I first saw this problem, I thought about a simple way to calculate the "score" of a string by comparing pairs of characters. The score calculation involves the absolute difference between each pair of characters, and I thought we could step through the string while maintaining a counter for our answer.\n\n## Approach\n1. Initialize two pointers, `i` and `j`, and an accumulator `ans` to store the final score.\n2. Iterate through the string using a loop where `i` runs from the start of the string to the second-last character.\n3. For each character at position `i`, increment the second pointer `j` by one.\n4. Calculate the absolute difference between the characters at positions `i` and `j` and add this difference to `ans`.\n5. Return the accumulated score `ans` after the loop completes.\n\n## Complexity\n- **Time complexity:** \\(O(n)\\)\n  - We traverse the string once, making the algorithm linear in relation to the size of the input string.\n- **Space complexity:** \\(O(1)\\)\n  - We only use a few integer variables for counting and summing, so the space usage is constant.\n\n## Code\n```java\nclass Solution {\n    public int scoreOfString(String s) {\n        int j = 0;\n        int ans = 0;\n        for (int i = 0; i < s.length() - 1; i++) {\n            j = i + 1;\n            ans += diff(s.charAt(i), s.charAt(j));\n        }\n        return ans;\n    }\n\n    int diff(int m, int n) {\n        return Math.abs(m - n);\n    }\n}\n```\n
['Java']