字符串评分程序


**字符串的评分**是一个用于根据字符串中相邻字符的ASCII值之间的绝对差之和来计算评分的概念。

问题陈述

给定一个字符串 s,计算字符串的评分。评分定义为相邻字符的ASCII值之间的绝对差之和。

示例场景 1

Input: s="abc"

Output: 2

s 中字符的ASCII值为 'a' = 97,'b' = 98,'c' = 99。因此,s 的评分 = |97-98|+|98-99|= 1+1 = 2 。

示例场景 2

Input: s="zaz"

Output: 50

s 中字符的ASCII值为 'z' = 122,'a' = 97。因此,s 的评分 = |122-97|+|97-122|=25+25=50。

示例场景 3

Input: s="nice"

Output: 13

s 中字符的ASCII值为 'n' = 110,'i' = 105,'c'=99,'e'=101。因此,s 的评分 = |110-105|+|105-99|+|99-101|+=5+6+2=13。

时间复杂度

计算字符串评分的时间复杂度为 O(n),其中 n 为字符串的长度。

为了用各种编程语言解决此问题,请使用以下方法。
  • 使用迭代方法
  • 使用列表推导式

使用迭代方法

迭代方法通过添加相邻字符的ASCII值之间的绝对差来计算字符串的评分。

示例

在此程序中,我们将评分初始化为 0,并从第二个字符迭代到字符串末尾。然后,我们计算每个字符的ASCII值与其前一个字符的ASCII值之间的绝对差,并将这些差值添加到评分中。

#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 = " << scoreOfString(s) << endl;
   return 0;
}
         

输出

Score of the string = 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 = " + scoreOfString(s));
   }
}
         

输出

Score of the string = 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(f"Score of the string = {score_of_string(s)}")
         

输出

Score of the string = 13

使用列表推导式

使用列表推导式为字符串评分涉及创建一种简洁有效的方法。此方法利用Python的列表推导式来遍历字符串并在单行代码中计算这些差异。

示例

此程序首先将相邻字符的ASCII值之间的绝对差存储在一个向量中,然后将这些差值加起来以获得最终评分,从而计算字符串的评分。main函数通过计算并打印字符串“nice”的评分来演示这一点。

示例

#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 = " << scoreOfString(s) << endl;
   return 0;
}
         

输出

Score of the string = 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 = " + scoreOfString(s));
   }
}
         

输出

Score of the string = 13
import math

def scoreOfString(s):
    diffs = []
    for i in range(1, len(s)):
        diffs.append(abs(ord(s[i]) - ord(s[i - 1])))
    score = sum(diffs)
    return score

s = "nice"
print(f"Score of the string = {scoreOfString(s)}")
         

输出

Score of the string = 13

Revathi Satya Kondra
Revathi Satya Kondra

Tutorialspoint 技术内容撰写人

更新于:2024年7月23日

209 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.