Check If Digits Are Equal in String After Operations I (LeetCode 3461): Solution and Explanation

Easy
Math
String
Simulation
Combinatorics
Number Theory

Each round replaces neighbouring digit pairs with their sum mod 10, building an upside-down triangle. Simulate it in place until two digits are left and compare them.

Intuition

Write the digits in a row, then write each pair's sum mod 10 in the gap below them. Every new row is one shorter, so the picture is an upside-down triangle that ends in two digits.

With at most 100 digits there are at most 98 rounds of at most 99 additions, a few thousand operations in total. Direct simulation is the natural answer.

No second array is needed. Walking left to right, d[i] is overwritten only after it has been read, and d[i + 1] still holds the previous round's value when it is used.

Approach: In-place simulation

  1. Convert s into an array of digits d.
  2. For each length from n down to 3, set d[i] = (d[i] + d[i + 1]) % 10 for every i from 0 to length - 2.
  3. After the last round the first two entries hold the final pair.
  4. Return d[0] == d[1].

Step-by-step example

s = "3902"
  1. Start: 3 9 0 2.
  2. Round 1: (3+9)%10 = 2, (9+0)%10 = 9, (0+2)%10 = 2, giving 2 9 2.
  3. Round 2: (2+9)%10 = 1, (9+2)%10 = 1, giving 1 1.
  4. Two digits remain and both are 1, so return true.

Code: Python, Java, C++

1class Solution:
2    def hasSameDigits(self, s: str) -> bool:
3        d = [int(c) for c in s]
4        for length in range(len(d), 2, -1):
5            for i in range(length - 1):
6                d[i] = (d[i] + d[i + 1]) % 10
7        return d[0] == d[1]
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n^2)

There are n - 2 rounds and each one does at most n - 1 additions.

Space

O(n)

One array of n digits, updated in place.

Other approaches

Build a new string each round (time O(n^2), space O(n))

Create the next row as a fresh string or list. Same time, slightly more allocation, and arguably easier to read.

Binomial coefficients mod 10 (time O(n log n), space O(n))

The final left digit is the sum of s[i] × C(n - 2, i) mod 10, and the right one uses s[i + 1]. Computing C mod 10 needs Lucas's theorem for 2 and 5 plus the Chinese remainder theorem. This is what the harder version of the problem with n up to 10^5 requires.

Common mistakes

  • Updating right to left in place. Then d[i + 1] has already been overwritten when d[i] needs its old value.
  • Running one round too many or too few. Stop when exactly two digits remain, which takes n - 2 rounds.
  • Comparing characters to integers, for example '3' against 3, after mixing string and number types.

Frequently asked questions

Why is the in-place update safe?

When computing the new d[i], you read d[i] and d[i + 1]. Going left to right, d[i + 1] has not been touched yet in this round, so it still holds the old value.

How does this relate to Pascal's triangle?

Each final digit is a weighted sum of the original digits, and the weights are the numbers in row n - 2 of Pascal's triangle, all taken mod 10.

Is the simulation fast enough?

Yes. For 100 digits it performs under 5,000 additions. Only the version with much longer strings needs the binomial approach.