✅ EASY WITH EXPLANATION || 💯 % on runtime 🚀
Intuition \uD83C\uDF1F\n\nWhen given a string, one natural way to measure how "different" consecutive characters are is to look at their ASCII values. By calcul
# Intuition \uD83C\uDF1F\n\nWhen given a string, one natural way to measure how "different" consecutive characters are is to look at their ASCII values. By calculating the absolute difference between the ASCII values of consecutive characters in the string, we can get a measure of how "different" the string is at each step. Summing these differences will give us the total score of the string.\n\n# Approach \uD83D\uDE80\n\nTo solve this problem, we can break down the solution into the following steps:\n\n1. **Initialize Variables**: Start by initializing a variable `ans` to store the cumulative score.\n2. **Iterate Through the String**: Loop through the string starting from the second character (index 1) to the end.\n3. **Calculate ASCII Differences**: For each character in the string (from the second character onwards), calculate the absolute difference between its ASCII value and the ASCII value of the previous character.\n4. **Accumulate the Differences**: Add each calculated absolute difference to the variable `ans`.\n5. **Return the Result**: After the loop finishes, `ans` will contain the total score of the string, which we then return.\n\nThis approach ensures that we consider the differences between all consecutive characters in the string, and by using the absolute value, we handle both positive and negative differences uniformly.\n\n# Complexity Analysis \uD83D\uDCCA\n\n- **Time Complexity**: The time complexity of this approach is **O(n)**, where **n** is the length of the string. This is because we have to iterate through the entire string once to calculate the differences.\n- **Space Complexity**: The space complexity is **O(1)** as we are using a constant amount of extra space regardless of the input string size.\n\n# Code \uD83D\uDDA5\uFE0F\n\n### Java \u2615\n```java\nclass Solution {\n    public int scoreOfString(String s) {\n        int ans = 0;\n        for(int i=1;i<s.length();i++){\n            ans += Math.abs(s.charAt(i-1)-s.charAt(i));\n        }\n        return ans;\n    }\n}\n```\n\n### JavaScript \uD83C\uDF10\n```javascript\n/**\n * @param {string} s\n * @return {number}\n */\nvar scoreOfString = (s) => {\n    let ans = 0;\n    for (let i = 1; i < s.length; i++) {\n        ans += Math.abs(s.charCodeAt(i - 1) - s.charCodeAt(i));\n    }\n    return ans;\n};\n```\n\n### Python \uD83D\uDC0D\n```python\nclass Solution(object):\n    def scoreOfString(self, s):\n        """\n        :type s: str\n        :rtype: int\n        """\n        ans = 0\n        for i in range(1, len(s), 1):\n            ans += abs(ord(s[i - 1]) - ord(s[i]))\n        return ans\n```\n\n### Python (Python 3) \uD83D\uDC0D\n```python\nclass Solution:\n    def scoreOfString(self, s: str) -> int:\n        ans = 0\n        for i in range(1, len(s), 1):\n            ans += abs(ord(s[i - 1]) - ord(s[i]))\n        return ans\n```\n\n### C++ \uD83D\uDCBB\n```cpp\nclass Solution {\npublic:\n    int scoreOfString(string s) {\n        int ans = 0;\n        for (int i = 1; i < s.size(); i++) {\n            ans += abs(s[i - 1] - s[i]);\n        }\n        return ans;\n    }\n};\n```
['String', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']