Easy C++ Solution | Linear Time | Constant Space | Beats 100 💯✅
Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to compute a score for a given string s, which is defined as the sum of the
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to compute a score for a given string `s`, which is defined as the sum of the absolute differences between the ASCII values of consecutive characters in the string.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Initialize a sum variable to 0**: This will store the cumulative score of the string.\n2. **Iterate through the string**: Loop through the string from the first character to the second-to-last character.\n3. **Compute absolute differences**: For each character, compute the absolute difference between its ASCII value and the ASCII value of the next character.\n4. **Update the sum**: Add this difference to the sum variable.\n5. **Return the sum**: Once the loop is complete, return the sum as the final score.\n\n# Complexity\n- Time complexity:  $$O(n)$$: The algorithm processes each character in the string exactly once. The main loop runs `n-1` times where `n` is the length of the string. Thus, the time complexity is linear with respect to the length of the string.\n\n- Space complexity: $$O(1)$$: The algorithm uses a constant amount of space regardless of the size of the input string. The only additional memory used is for the integer variables (`sum` and `n`), which do not depend on the length of the string.\n\n# Code\n```\nclass Solution {\npublic:\n    int scoreOfString(string s) {\n        int sum=0;\n        int n=s.size();\n        for(int i=0;i<n-1;i++)\n            sum+=abs(s[i]-s[i+1]);\n        return sum;\n    }\n};\n```
['String', 'C++']