⬆️🔥 Beats 100% | Space O(1) | Time O(n) | Python3 | Easy ✅
Submission\n\n\n\n# Intuition\n\nThe question is to calculate the sum of the absolute difference between every two consecutive elements\' ASCII value, so you ne
# Submission\n\n![leetcodesos.png](https://assets.leetcode.com/users/images/1684f7f6-72f9-49c6-8971-4dd27de55b6c_1713202493.683806.png)\n\n# Intuition\n\nThe question is to calculate the sum of the absolute difference between every two consecutive elements\' `ASCII` value, so you need to iterate through strings using the `for` (or) `while` loop.\n\nTo calculate the absolute difference between two numbers in Python, you can use the abs() function. Here\'s how you can do it:\n\n```\nabsolute_difference = abs(num1 - num2)\n```\n\nIn Python, the ord() function is used to retrieve the ASCII value (Unicode code point) of a single character. It takes a single character (a string of length 1) as an argument and returns an integer representing its Unicode code point.\n```\nchar = \'A\'\nascii_value = ord(char)\n```\n\n##### Note: \nIn Python, the abs() function is a built-in function used to return the absolute value of a number. The absolute value of a number is its distance from zero on the number line, regardless of its sign. \n\n\n# Approach\n1. First, initialize and assign the initial value of the score variable to zero.\n\n2. Run the loop until `length -1 times`, so from index zero to index length of s - 2 , which is :\n\n```\nfor i in range(len(s)-1)\n```\n3. Each time you iterate, update the score value by the absolute difference between the `s[i] and s[i+1]` elements\' ASCII values in string s.\n```\nscore += abs(ord(s[i]) - ord(s[i+1]))\n```\n4. Now, finally, `return` the score outside the loop.\n\n# Complexity\n\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution:\n    def scoreOfString(self, s: str) -> int:\n\n        score = 0\n        for i in range(len(s)-1):\n            score += abs(ord(s[i]) - ord(s[i+1]))\n        \n        return score\n        \n```
['String', 'Python3']