You are given a string s. The score of a string is defined as the sum of the absolute difference between the ASCII values of adjacent characters.Return the score of s. Example 1:Input: s = "hello"Output: 13Explanation:The ASCII values of the characters in s are: 'h' = 104, 'e' = 101, 'l' = 108, 'o' = 111. So, the score of s would be |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13.Example 2:Input: s = "zaz"Output: 50Explanation:The ASCII values of the characters in s are: 'z' = 122, 'a' = 97. So, the score of s would be |122 - 97| + |97 - 122| = 25 + 25 = 50. Constraints:2 <= s.length <= 100s consists only of lowercase English letters.
Question
You are given a string s. The score of a string is defined as the sum of the absolute difference between the ASCII values of adjacent characters.Return the score of s. Example 1:Input: s = "hello"Output: 13Explanation:The ASCII values of the characters in s are: 'h' = 104, 'e' = 101, 'l' = 108, 'o' = 111. So, the score of s would be |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13.Example 2:Input: s = "zaz"Output: 50Explanation:The ASCII values of the characters in s are: 'z' = 122, 'a' = 97. So, the score of s would be |122 - 97| + |97 - 122| = 25 + 25 = 50. Constraints:2 <= s.length <= 100s consists only of lowercase English letters.
Solution
To solve this problem, you can follow these steps:
-
Initialize a variable
scoreto 0. This will hold the total score of the string. -
Convert the string
sinto a list of characters. -
Iterate over the list of characters. For each character, get its ASCII value using the
ord()function in Python. -
Calculate the absolute difference between the ASCII value of the current character and the ASCII value of the next character. Add this difference to
score. -
Continue this process until you have iterated over all the characters in the string.
-
Return
scoreas the final score of the string.
Here is a Python code snippet that implements these steps:
def string_score(s):
score = 0
for i in range(len(s) - 1):
score += abs(ord(s[i]) - ord(s[i+1]))
return score
You can call this function with a string to get its score. For example:
print(string_score("hello")) # Output: 13
print(string_score("zaz")) # Output: 50
This function works for strings of length 2 to 100 and consisting only of lowercase English letters, as per the problem constraints.
Similar Questions
Max Score: 20Given a string S and a character C, check whether the character C is occurring together in the string S.Input FormatThe first and only line of input contains a string S and character, C separated by space.Output FormatPrint "Yes" if the character C is occurring together, otherwise print "No".Constraints1 <= |S| <= 103ExampleInputsnooze oOutputYes
Input format :The first line contains an integer 't' representing the number of test cases.Each test case consists of a single line with two space-separated integers 'x' and 'y', representing the scores of Sasha and Brenda, respectively, for a particular round.Output format :For each test case, the output prints the result of that round.If x is greater than y, the output prints "Sasha".If x is equal to y, the output prints "Tie".Otherwise, the output prints "Brenda"
Write a Java program that reads an unspecified number of scores anddetermines how many scores are above or equal to the average, and how many scoresare below the average. Enter a negative number to signify the end of the input. Assumethe maximum number of scores is 100.
Select the correct answerWhat is the output of the following program?public class Score{ public static void main(String[] args) { System.out.println((125/50.0)*Integer.parseInt("10") + 70); }}Options95.080Compilation errorRuntime error
Develop a program with a method to print the count of a given character in a string and return the sum of the respective indices.Hint: use charAt method to fetch a character in a string. String s="apple"; s.charAt(0) fetches the first character.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.