|
| 1 | +# [3110. 字符串的分数](https://leetcode.cn/problems/score-of-a-string) |
| 2 | + |
| 3 | +[English Version](/solution/3100-3199/3110.Score%20of%20a%20String/README_EN.md) |
| 4 | + |
| 5 | +<!-- tags: --> |
| 6 | + |
| 7 | +## 题目描述 |
| 8 | + |
| 9 | +<!-- 这里写题目描述 --> |
| 10 | + |
| 11 | +<p>给你一个字符串 <code>s</code> 。一个字符串的 <strong>分数</strong> 定义为相邻字符 <strong>ASCII</strong> 码差值绝对值的和。</p> |
| 12 | + |
| 13 | +<p>请你返回 <code>s</code> 的 <strong>分数</strong> 。</p> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | + |
| 17 | +<p><strong class="example">示例 1:</strong></p> |
| 18 | + |
| 19 | +<div class="example-block"> |
| 20 | +<p><span class="example-io"><b>输入:</b>s = "hello"</span></p> |
| 21 | + |
| 22 | +<p><span class="example-io"><b>输出:</b>13</span></p> |
| 23 | + |
| 24 | +<p><strong>解释:</strong></p> |
| 25 | + |
| 26 | +<p><code>s</code> 中字符的 <strong>ASCII </strong>码分别为:<code>'h' = 104</code> ,<code>'e' = 101</code> ,<code>'l' = 108</code> ,<code>'o' = 111</code> 。所以 <code>s</code> 的分数为 <code>|104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13</code> 。</p> |
| 27 | +</div> |
| 28 | + |
| 29 | +<p><strong class="example">示例 2:</strong></p> |
| 30 | + |
| 31 | +<div class="example-block"> |
| 32 | +<p><span class="example-io"><b>输入:</b>s = "zaz"</span></p> |
| 33 | + |
| 34 | +<p><span class="example-io"><b>输出:</b>50</span></p> |
| 35 | + |
| 36 | +<p><strong>解释:</strong></p> |
| 37 | + |
| 38 | +<p><code>s</code> 中字符的 <strong>ASCII </strong>码分别为:<code>'z' = 122</code> ,<code>'a' = 97</code> 。所以 <code>s</code> 的分数为 <code>|122 - 97| + |97 - 122| = 25 + 25 = 50</code> 。</p> |
| 39 | +</div> |
| 40 | + |
| 41 | +<p> </p> |
| 42 | + |
| 43 | +<p><strong>提示:</strong></p> |
| 44 | + |
| 45 | +<ul> |
| 46 | + <li><code>2 <= s.length <= 100</code></li> |
| 47 | + <li><code>s</code> 只包含小写英文字母。</li> |
| 48 | +</ul> |
| 49 | + |
| 50 | +## 解法 |
| 51 | + |
| 52 | +### 方法一:模拟 |
| 53 | + |
| 54 | +我们直接遍历字符串 $s$,计算相邻字符的 ASCII 码差值的绝对值之和即可。 |
| 55 | + |
| 56 | +时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。 |
| 57 | + |
| 58 | +<!-- tabs:start --> |
| 59 | + |
| 60 | +```python |
| 61 | +class Solution: |
| 62 | + def scoreOfString(self, s: str) -> int: |
| 63 | + return sum(abs(a - b) for a, b in pairwise(map(ord, s))) |
| 64 | +``` |
| 65 | + |
| 66 | +```java |
| 67 | +class Solution { |
| 68 | + public int scoreOfString(String s) { |
| 69 | + int ans = 0; |
| 70 | + for (int i = 1; i < s.length(); ++i) { |
| 71 | + ans += Math.abs(s.charAt(i - 1) - s.charAt(i)); |
| 72 | + } |
| 73 | + return ans; |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +```cpp |
| 79 | +class Solution { |
| 80 | +public: |
| 81 | + int scoreOfString(string s) { |
| 82 | + int ans = 0; |
| 83 | + for (int i = 1; i < s.size(); ++i) { |
| 84 | + ans += abs(s[i] - s[i - 1]); |
| 85 | + } |
| 86 | + return ans; |
| 87 | + } |
| 88 | +}; |
| 89 | +``` |
| 90 | +
|
| 91 | +```go |
| 92 | +func scoreOfString(s string) (ans int) { |
| 93 | + for i := 1; i < len(s); i++ { |
| 94 | + ans += abs(int(s[i-1]) - int(s[i])) |
| 95 | + } |
| 96 | + return |
| 97 | +} |
| 98 | +
|
| 99 | +func abs(x int) int { |
| 100 | + if x < 0 { |
| 101 | + return -x |
| 102 | + } |
| 103 | + return x |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +```ts |
| 108 | +function scoreOfString(s: string): number { |
| 109 | + let ans = 0; |
| 110 | + for (let i = 1; i < s.length; ++i) { |
| 111 | + ans += Math.abs(s.charCodeAt(i) - s.charCodeAt(i - 1)); |
| 112 | + } |
| 113 | + return ans; |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +<!-- tabs:end --> |
| 118 | + |
| 119 | +<!-- end --> |
0 commit comments