Skip to content

Commit 8cbb9a2

Browse files
committed
[LeetCode Sync] Runtime - 23 ms (93.56%), Memory - 17.9 MB (26.37%)
1 parent 390729c commit 8cbb9a2

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<p>You are given a string <code>s</code> consisting of digits. Perform the following operation repeatedly until the string has <strong>exactly</strong> two digits:</p>
2+
3+
<ul>
4+
<li>For each pair of consecutive digits in <code>s</code>, starting from the first digit, calculate a new digit as the sum of the two digits <strong>modulo</strong> 10.</li>
5+
<li>Replace <code>s</code> with the sequence of newly calculated digits, <em>maintaining the order</em> in which they are computed.</li>
6+
</ul>
7+
8+
<p>Return <code>true</code> if the final two digits in <code>s</code> are the <strong>same</strong>; otherwise, return <code>false</code>.</p>
9+
10+
<p>&nbsp;</p>
11+
<p><strong class="example">Example 1:</strong></p>
12+
13+
<div class="example-block">
14+
<p><strong>Input:</strong> <span class="example-io">s = &quot;3902&quot;</span></p>
15+
16+
<p><strong>Output:</strong> <span class="example-io">true</span></p>
17+
18+
<p><strong>Explanation:</strong></p>
19+
20+
<ul>
21+
<li>Initially, <code>s = &quot;3902&quot;</code></li>
22+
<li>First operation:
23+
<ul>
24+
<li><code>(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2</code></li>
25+
<li><code>(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9</code></li>
26+
<li><code>(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2</code></li>
27+
<li><code>s</code> becomes <code>&quot;292&quot;</code></li>
28+
</ul>
29+
</li>
30+
<li>Second operation:
31+
<ul>
32+
<li><code>(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1</code></li>
33+
<li><code>(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1</code></li>
34+
<li><code>s</code> becomes <code>&quot;11&quot;</code></li>
35+
</ul>
36+
</li>
37+
<li>Since the digits in <code>&quot;11&quot;</code> are the same, the output is <code>true</code>.</li>
38+
</ul>
39+
</div>
40+
41+
<p><strong class="example">Example 2:</strong></p>
42+
43+
<div class="example-block">
44+
<p><strong>Input:</strong> <span class="example-io">s = &quot;34789&quot;</span></p>
45+
46+
<p><strong>Output:</strong> <span class="example-io">false</span></p>
47+
48+
<p><strong>Explanation:</strong></p>
49+
50+
<ul>
51+
<li>Initially, <code>s = &quot;34789&quot;</code>.</li>
52+
<li>After the first operation, <code>s = &quot;7157&quot;</code>.</li>
53+
<li>After the second operation, <code>s = &quot;862&quot;</code>.</li>
54+
<li>After the third operation, <code>s = &quot;48&quot;</code>.</li>
55+
<li>Since <code>&#39;4&#39; != &#39;8&#39;</code>, the output is <code>false</code>.</li>
56+
</ul>
57+
</div>
58+
59+
<p>&nbsp;</p>
60+
<p><strong>Constraints:</strong></p>
61+
62+
<ul>
63+
<li><code>3 &lt;= s.length &lt;= 100</code></li>
64+
<li><code>s</code> consists of only digits.</li>
65+
</ul>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def hasSameDigits(self, s: str) -> bool:
3+
temp_list = list(map(int, s))
4+
n = len(s)
5+
for i in range(n - 1, 1, -1):
6+
for j in range(i):
7+
temp_list[j] = (temp_list[j] + temp_list[j + 1]) % 10
8+
return temp_list[0] == temp_list[1]

0 commit comments

Comments
 (0)