|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3461.Check%20If%20Digits%20Are%20Equal%20in%20String%20After%20Operations%20I/README_EN.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3461. Check If Digits Are Equal in String After Operations I](https://leetcode.com/problems/check-if-digits-are-equal-in-string-after-operations-i) |
| 10 | + |
| 11 | +[中文文档](/solution/3400-3499/3461.Check%20If%20Digits%20Are%20Equal%20in%20String%20After%20Operations%20I/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<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> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <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> |
| 21 | + <li>Replace <code>s</code> with the sequence of newly calculated digits, <em>maintaining the order</em> in which they are computed.</li> |
| 22 | +</ul> |
| 23 | + |
| 24 | +<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> |
| 25 | + |
| 26 | +<p> </p> |
| 27 | +<p><strong class="example">Example 1:</strong></p> |
| 28 | + |
| 29 | +<div class="example-block"> |
| 30 | +<p><strong>Input:</strong> <span class="example-io">s = "3902"</span></p> |
| 31 | + |
| 32 | +<p><strong>Output:</strong> <span class="example-io">true</span></p> |
| 33 | + |
| 34 | +<p><strong>Explanation:</strong></p> |
| 35 | + |
| 36 | +<ul> |
| 37 | + <li>Initially, <code>s = "3902"</code></li> |
| 38 | + <li>First operation: |
| 39 | + <ul> |
| 40 | + <li><code>(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2</code></li> |
| 41 | + <li><code>(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9</code></li> |
| 42 | + <li><code>(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2</code></li> |
| 43 | + <li><code>s</code> becomes <code>"292"</code></li> |
| 44 | + </ul> |
| 45 | + </li> |
| 46 | + <li>Second operation: |
| 47 | + <ul> |
| 48 | + <li><code>(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1</code></li> |
| 49 | + <li><code>(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1</code></li> |
| 50 | + <li><code>s</code> becomes <code>"11"</code></li> |
| 51 | + </ul> |
| 52 | + </li> |
| 53 | + <li>Since the digits in <code>"11"</code> are the same, the output is <code>true</code>.</li> |
| 54 | +</ul> |
| 55 | +</div> |
| 56 | + |
| 57 | +<p><strong class="example">Example 2:</strong></p> |
| 58 | + |
| 59 | +<div class="example-block"> |
| 60 | +<p><strong>Input:</strong> <span class="example-io">s = "34789"</span></p> |
| 61 | + |
| 62 | +<p><strong>Output:</strong> <span class="example-io">false</span></p> |
| 63 | + |
| 64 | +<p><strong>Explanation:</strong></p> |
| 65 | + |
| 66 | +<ul> |
| 67 | + <li>Initially, <code>s = "34789"</code>.</li> |
| 68 | + <li>After the first operation, <code>s = "7157"</code>.</li> |
| 69 | + <li>After the second operation, <code>s = "862"</code>.</li> |
| 70 | + <li>After the third operation, <code>s = "48"</code>.</li> |
| 71 | + <li>Since <code>'4' != '8'</code>, the output is <code>false</code>.</li> |
| 72 | +</ul> |
| 73 | +</div> |
| 74 | + |
| 75 | +<p> </p> |
| 76 | +<p><strong>Constraints:</strong></p> |
| 77 | + |
| 78 | +<ul> |
| 79 | + <li><code>3 <= s.length <= 100</code></li> |
| 80 | + <li><code>s</code> consists of only digits.</li> |
| 81 | +</ul> |
| 82 | + |
| 83 | +<!-- description:end --> |
| 84 | + |
| 85 | +## Solutions |
| 86 | + |
| 87 | +<!-- solution:start --> |
| 88 | + |
| 89 | +### Solution 1 |
| 90 | + |
| 91 | +<!-- tabs:start --> |
| 92 | + |
| 93 | +#### Python3 |
| 94 | + |
| 95 | +```python |
| 96 | + |
| 97 | +``` |
| 98 | + |
| 99 | +#### Java |
| 100 | + |
| 101 | +```java |
| 102 | + |
| 103 | +``` |
| 104 | + |
| 105 | +#### C++ |
| 106 | + |
| 107 | +```cpp |
| 108 | + |
| 109 | +``` |
| 110 | + |
| 111 | +#### Go |
| 112 | + |
| 113 | +```go |
| 114 | + |
| 115 | +``` |
| 116 | + |
| 117 | +<!-- tabs:end --> |
| 118 | + |
| 119 | +<!-- solution:end --> |
| 120 | + |
| 121 | +<!-- problem:end --> |
0 commit comments