Skip to main content

Check If Digits Are Equal in String After Operations I - Solution & Explanation

EasyMathStringSimulationCombinatorics6 min readAsked at: Amazon, Microsoft, Meta +1
Practice this problem

Problem Statement

You are given a string s consisting of digits. Perform the following operation repeatedly until the string has exactly two digits:

  • For each pair of consecutive digits in s, starting from the first digit, calculate a new digit as the sum of the two digits modulo 10.
  • Replace 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:

  • Initially, s = "3902"
  • First operation:
    • (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 = 2
    • s becomes "292"
  • Second operation:
    • (s[0] + s[1]) % 10 = (2 + 9) % 10 = 1
    • (s[1] + s[2]) % 10 = (9 + 2) % 10 = 1
    • s becomes "11"
  • Since the digits in "11" are the same, the output is true.

Example 2:

Input: s = "34789"

Output: false

Explanation:

  • Initially, s = "34789".
  • After the first operation, s = "7157".
  • After the second operation, s = "862".
  • After the third operation, s = "48".
  • Since '4' != '8', the output is false.

 

Constraints:

  • 3 <= s.length <= 100
  • s consists of only digits.

Approach Overview

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.

Solution

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.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Direct Simulation with New ArrayO(n^2)O(n)Best for clarity and quick implementation that follows the problem statement step by step
In‑Place SimulationO(n^2)O(1)When you want the same logic but with constant extra memory

Video Solution

3463 & 3461 Check If Digits Are Equal in String After Operations II | nCr | Luca's Theorem | PascalsAryan Mittal6,576 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Check If Digits Are Equal in String After Operations I easy or hard?
The problem is classified as Easy. The main challenge is recognizing that the operation simply reduces the array step by step, which can be implemented directly with a simulation loop.
Check If Digits Are Equal in String After Operations I Python/Java solution
The solution converts the string to a list of integers and simulates the reduction process. For each round, compute (digits[i] + digits[i+1]) % 10 and overwrite or build the next list until only two digits remain. The same logic works in Python, Java, C++, Go, and TypeScript with O(n^2) time complexity.
How to solve Check If Digits Are Equal in String After Operations I in O(n)?
Most implementations use O(n^2) simulation because the string shrinks gradually. A theoretical O(n) method can be derived by expressing the final digits as weighted sums using binomial coefficients modulo 10, but that level of optimization is usually unnecessary for this version of the problem.
What is the best approach for Check If Digits Are Equal in String After Operations I?
The most practical approach is simulation. Convert the string into digits and repeatedly replace each adjacent pair with (a + b) % 10 until only two digits remain. An in‑place simulation keeps the time complexity at O(n^2) while reducing space usage to O(1).
Is Check If Digits Are Equal in String After Operations I asked at Google/Amazon/Meta?
Problems involving digit transformations and iterative reductions appear in interviews at companies like Google, Amazon, and Meta. Variants test your ability to simulate processes on arrays or strings and sometimes recognize the combinatorial pattern behind repeated pair operations.
What data structure is used in Check If Digits Are Equal in String After Operations I?
The core structure is a simple array or list of digits derived from the input string. The algorithm repeatedly iterates over this array, updating each position with the sum of adjacent digits modulo 10.
What is the time complexity of Check If Digits Are Equal in String After Operations I?
The standard simulation runs in O(n^2) time. Each operation shortens the string by one, and every step processes the remaining digits, producing roughly n + (n−1) + ... + 2 computations. Space complexity ranges from O(n) with a temporary array to O(1) with an in‑place implementation.

Ready to solve this problem?

Practice Check If Digits Are Equal in String After Operations I with our built-in code editor and test cases.

Practice on FleetCode