You are given a string s consisting of digits. Perform the following operation repeatedly until the string has exactly two digits:
s, starting from the first digit, calculate a new digit as the sum of the two digits modulo 10.s with the sequence of newly calculated digits, maintaining the order in which they are computed.Return true if the final two digits in s are the same; otherwise, return false.
Example 1:
Input: s = "3902"
Output: true
Explanation:
s = "3902"(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2s becomes "292"(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1s becomes "11""11" are the same, the output is true.Example 2:
Input: s = "34789"
Output: false
Explanation:
s = "34789".s = "7157".s = "862".s = "48".'4' != '8', the output is false.
Constraints:
3 <= s.length <= 100s consists of only digits.Problem Overview: You get a string of digits. In one operation, replace every adjacent pair (a, b) with (a + b) % 10, forming a new string that is one character shorter. Repeat until only two digits remain. The task is to check whether those final two digits are equal.
Approach 1: Direct Simulation (O(n^2) time, O(n) space)
Store the digits in an array and repeatedly simulate the described operation. For each step, iterate through the current array and compute (digits[i] + digits[i+1]) % 10 for every adjacent pair, building the next array. Each iteration reduces the length by one, so the process runs roughly n + (n-1) + ... + 2 operations. The logic mirrors the problem statement exactly, which makes it easy to reason about and implement. This approach fits naturally with string processing and simulation techniques.
Approach 2: In‑Place Simulation (O(n^2) time, O(1) space)
You can avoid allocating a new array at every step. Convert the string into a mutable list of digits and reuse the same array. During each round, iterate from left to right and overwrite digits[i] with (digits[i] + digits[i+1]) % 10. After finishing a pass, treat the effective length as one smaller. The computation still performs the same number of arithmetic operations, but memory usage drops to constant space because the transformation happens in place. This technique is common when implementing iterative transformations on arrays or strings.
The math behind the repeated reductions forms a triangular pattern similar to Pascal's triangle, which connects to math and combinatorial reasoning. However, for this version of the problem the constraints are small enough that straightforward simulation is the simplest and most practical solution.
Recommended for interviews: The in-place simulation is typically preferred. It demonstrates that you understand the transformation process and can optimize memory usage while keeping the logic clear. Starting with the basic simulation also shows solid problem‑solving steps before refining the implementation.
We can simulate the operations described in the problem until the string s contains exactly two digits, and then check if these two digits are the same.
The time complexity is O(n^2), and the space complexity is O(n). Here, n is the length of the string s.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Simulation with New Array | O(n^2) | O(n) | Best for clarity and quick implementation that follows the problem statement step by step |
| In‑Place Simulation | O(n^2) | O(1) | When you want the same logic but with constant extra memory |
3463 & 3461 Check If Digits Are Equal in String After Operations II | nCr | Luca's Theorem | Pascals • Aryan Mittal • 6,290 views views
Watch 9 more video solutions →Practice Check If Digits Are Equal in String After Operations I with our built-in code editor and test cases.
Practice on FleetCode