Program to find a Score of a String


The Score of a String is a concept used to calculate the score based on the sum of the absolute differences between the ASCII values of adjacent characters in the string.

Problem Statement

Given a string s, calculate the score of the string. The score is defined as the sum of the absolute differences between the ASCII values of adjacent characters.

Example Scenario 1

Input: s="abc"

Output: 2

The ASCII values of the characters in s are 'a' = 97, 'b' = 98, 'c' = 99. So, the score of s = |97-98|+|98-99|= 1+1 = 2 .

Example Scenario 2

Input: s="zaz"

Output: 50

The ASCII values of the characters in s are 'z' = 122, 'a' = 97. So, the score of s = |122-97|+|97-122|=25+25=50.

Example Scenario 3

Input: s="nice"

Output: 13

The ASCII values of the characters in s are 'n' = 110, 'i' = 105, 'c'=99, 'e'=101 So, the score of s = |110-105|+|105-99|+|99-101|+=5+6+2=13.

Time Complexity

The time complexity of calculating the score of a string is O(n), where n is the length of the string.

To solve this problem in various programming languages, use the following methods.
  • Using Iterative Method
  • Using List Comprehension

Using Iterative Method

The iterative method calculates the score of a string by adding the absolute difference between the ASCII values of adjacent characters.

Example

In this program, we initialize the score to 0 and iterate through the string from the second character to the end. We then calculate the absolute difference between each character's ASCII value and the previous character's ASCII value, adding these differences to the score.

#include <iostream>
#include <cmath>
using namespace std;

int scoreOfString(string s) {
   int score = 0;
   for (int i = 1; i < s.length(); ++i) {
       score += abs(s[i] - s[i - 1]);
   }
   return score;
}

int main() {
   string s = "nice";
   cout << "Score of the string "" << s << "" = " << scoreOfString(s) << endl;
   return 0;
}
         

Output

Score of the string "nice" = 13
public class ScoreOfString {
   public static int scoreOfString(String s) {
      int score = 0;
      for (int i = 1; i < s.length(); i++) {
          score += Math.abs(s.charAt(i) - s.charAt(i - 1));
      }
      return score;
   }
   
   public static void main(String[] args) {
      String s = "nice";
      System.out.println("Score of the string "" + s + "" = " + scoreOfString(s));
   }
}
         

Output

Score of the string "nice" = 13
def score_of_string(s):
   score = 0
   for i in range(1, len(s)):
      score += abs(ord(s[i]) - ord(s[i - 1]))
   return score

s = "nice"
print("Score of the string "{}" = {}".format(s, score_of_string(s)))
         

Output

Score of the string "nice" = 13

Using List Comprehension

Using list comprehension to score a string involves creating a concise and efficient way. This method leverages Python’s list comprehension to iterate through the string and compute these differences in a single line of code.

Example

This program calculates the score of a string by first storing the absolute differences between the ASCII values of adjacent characters in a vector, then aading these differences to get the final score. The main function demonstrates this by calculating and printing the score for the string "nice".

Example

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

int scoreOfString(string s) {
   vector<int> diffs;
   for (int i = 1; i < s.length(); ++i) {
      diffs.push_back(abs(s[i] - s[i - 1]));
   }
   int score = 0;
   for (int diff : diffs) {
      score += diff;
   }
   return score;
}

int main() {
   string s = "nice";
   cout << "Score of the string "" << s << "" = " << scoreOfString(s) << endl;
   return 0;
}
         

Output

Score of the string "nice" = 13
import java.util.stream.IntStream;

public class ScoreOfString {
   public static int scoreOfString(String s) {
      return IntStream.range(1, s.length())
                      .map(i -> Math.abs(s.charAt(i) - s.charAt(i - 1)))
                      .sum();
   }

   public static void main(String[] args) {
      String s = "nice";
      System.out.println("Score of the string "" + s + "" = " + scoreOfString(s));
   }
}
         

Output

Score of the string "nice" = 13
def score_of_string(s):
   return sum(abs(ord(s[i]) - ord(s[i - 1])) for i in range(1, len(s)))

s = "nice"
print("Score of the string "{}" = {}".format(s, score_of_string(s)))
         

Output

Score of the string "nice" = 13

Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 18-Jul-2024

0 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements