mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
37 lines
1.6 KiB
HTML
37 lines
1.6 KiB
HTML
<p>You are given a string <code>s</code>. The <strong>score</strong> of a string is defined as the sum of the absolute difference between the <strong>ASCII</strong> values of adjacent characters.</p>
|
|
|
|
<p>Return the <strong>score</strong> of<em> </em><code>s</code>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "hello"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">13</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<p>The <strong>ASCII</strong> values of the characters in <code>s</code> are: <code>'h' = 104</code>, <code>'e' = 101</code>, <code>'l' = 108</code>, <code>'o' = 111</code>. So, the score of <code>s</code> would be <code>|104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13</code>.</p>
|
|
</div>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "zaz"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">50</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<p>The <strong>ASCII</strong> values of the characters in <code>s</code> are: <code>'z' = 122</code>, <code>'a' = 97</code>. So, the score of <code>s</code> would be <code>|122 - 97| + |97 - 122| = 25 + 25 = 50</code>.</p>
|
|
</div>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>2 <= s.length <= 100</code></li>
|
|
<li><code>s</code> consists only of lowercase English letters.</li>
|
|
</ul>
|