🔥✅Beat 100%✅|| C/C++/Java/JavaScript/Python || Full explanation✅|| Very easy approach/ Technique✅🔥
Please upvote my solution and give a star if you like the solution...\uD83D\uDE0A\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n
# Please upvote my solution and give a star if you like the solution...\uD83D\uDE0A\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem "Score of a String" requires us to calculate the total score based on the differences between consecutive characters in the string. The intuition here is to interpret the score as the sum of absolute differences between the ASCII values of each pair of consecutive characters. This allows us to capture the "distance" or "change" between adjacent characters.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Initialization: Start with a score variable initialized to 0.\n- Iteration: Loop through the string from the first character to the second-last character.\n- Calculation: For each character, calculate the absolute difference between its ASCII value and the ASCII value of the next character.\n- Accumulation: Add this difference to the score.\n- Return Result: After processing all consecutive character pairs, return the score.\n \nThe approach ensures that we efficiently calculate the desired score by leveraging a simple linear scan and difference computation.\n\n# Detailed Steps:\n- Initialize score to 0.\n- Loop through the string using an index i from 0 to s.length() - 2.\n- For each character s[i], compute the absolute difference with s[i + 1].\n- Add this absolute difference to score.\n- After the loop, return the accumulated score.\n\n# Complexity\n- Time complexity: O(n), where n is the length of the string. This is because we iterate through the string exactly once.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1), as we only use a fixed amount of extra space regardless of the input string size. The space required for the score variable does not scale with the input size.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C []\nint scoreOfString(char* s) {\n    int score = 0;\n    for (int i = 0; s[i + 1] != \'\\0\'; i++) {\n        score = score + abs(s[i] - s[i + 1]);\n    }\n    return score;\n}\n\n```\n```C++ []\nclass Solution {\npublic:\n    int scoreOfString(string s) {\n        int score = 0;\n        for (int i = 0; i < s.length() - 1; i++) {\n            score = score + abs(int(s[i]) - int(s[i + 1]));\n        }\n        return score;\n    }\n};\n```\n```Java []\npublic int scoreOfString(String s) {\n    int score = 0;\n    for (int i = 0; i < s.length() - 1; i++) {\n        score = score + Math.abs(s.charAt(i) - s.charAt(i + 1));\n    }\n    return score;\n}\n\n```\n```JavaScript []\nfunction scoreOfString(s) {\n    let score = 0;\n    for (let i = 0; i < s.length - 1; i++) {\n        score = score + Math.abs(s.charCodeAt(i) - s.charCodeAt(i + 1));\n    }\n    return score;\n}\n\n```\n```Python []\ndef scoreOfString(s):\n    score = 0\n    for i in range(len(s) - 1):\n        score += abs(ord(s[i]) - ord(s[i + 1]))\n    return score\n\n```\n\n# Conclusion\nThis solution provides an efficient and straightforward method to calculate the score of a string based on the differences between consecutive characters. It operates in linear time, making it suitable for large inputs, and uses constant space, ensuring minimal memory overhead. This clear and efficient approach should be easy to understand and implement, making it an ideal solution for the problem.\n\n![e76411b8-a184-4974-8098-a5792b2667a8_1708225665.6591249.png](https://assets.leetcode.com/users/images/a422c67b-1dbc-4081-ac01-69ecd6172249_1717220218.3712153.png)\n
['C', 'Python', 'C++', 'Java', 'JavaScript']