Skip to content

Commit ce75b76

Browse files
committed
Sync LeetCode submission Runtime - 36 ms (59.23%), Memory - 16.6 MB (13.24%)
1 parent bd73bea commit ce75b76

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

3379-score-of-a-string/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<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>
2+
3+
<p>Return the <strong>score</strong> of<em> </em><code>s</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<div class="example-block">
9+
<p><strong>Input:</strong> <span class="example-io">s = &quot;hello&quot;</span></p>
10+
11+
<p><strong>Output:</strong> <span class="example-io">13</span></p>
12+
13+
<p><strong>Explanation:</strong></p>
14+
15+
<p>The <strong>ASCII</strong> values of the characters in <code>s</code> are: <code>&#39;h&#39; = 104</code>, <code>&#39;e&#39; = 101</code>, <code>&#39;l&#39; = 108</code>, <code>&#39;o&#39; = 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>
16+
</div>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<div class="example-block">
21+
<p><strong>Input:</strong> <span class="example-io">s = &quot;zaz&quot;</span></p>
22+
23+
<p><strong>Output:</strong> <span class="example-io">50</span></p>
24+
25+
<p><strong>Explanation:</strong></p>
26+
27+
<p>The <strong>ASCII</strong> values of the characters in <code>s</code> are: <code>&#39;z&#39; = 122</code>, <code>&#39;a&#39; = 97</code>. So, the score of <code>s</code> would be <code>|122 - 97| + |97 - 122| = 25 + 25 = 50</code>.</p>
28+
</div>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>2 &lt;= s.length &lt;= 100</code></li>
35+
<li><code>s</code> consists only of lowercase English letters.</li>
36+
</ul>

3379-score-of-a-string/solution.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Approach 1: Lineaar Iteration
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def scoreOfString(self, s: str) -> int:
8+
score = 0
9+
10+
for i in range(len(s) - 1):
11+
score += abs(ord(s[i]) - ord(s[i + 1]))
12+
13+
return score
14+

0 commit comments

Comments
 (0)