LeetCode Problem Workspace
Check If Digits Are Equal in String After Operations I
Simulate repeated adjacent digit sums modulo 10 until two digits remain, then check whether those final digits match.
5
Topics
5
Code langs
3
Related
Practice Focus
Easy · Math plus String
Answer-first summary
Simulate repeated adjacent digit sums modulo 10 until two digits remain, then check whether those final digits match.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Math plus String
For Check If Digits Are Equal in String After Operations I, the cleanest solution is direct simulation. Repeatedly build the next string by replacing each adjacent pair with their sum modulo 10, then stop when two digits remain and compare them. Since the length is at most 100, the straightforward Math plus String approach is fast enough and matches the operation exactly.
Problem Statement
You receive a digit string s. While its length is greater than 2, create a new string where each position is formed from the sum of two neighboring digits in the current string, taken modulo 10. Keep shrinking the string with this rule until only two digits are left.
After all operations finish, return true when the last two digits are equal and false otherwise. For example, s = "3902" reduces through the required adjacent-sum process to two equal digits, while s = "34789" ends with two different digits.
Examples
Example 1
Input: s = "3902"
Output: true
Example 2
Input: s = "34789"
Output: false
Constraints
- 3 <= s.length <= 100
- s consists of only digits.
Solution Approach
Model the operation exactly
Treat the problem as pure simulation. Convert each character to its numeric digit, then for every pass compute a new sequence where next[i] = (curr[i] + curr[i + 1]) % 10. This matches the problem rule directly and avoids overthinking an Easy problem whose main test is careful implementation.
Shrink until two digits remain
Each operation shortens the string by one, so a simple loop runs until the array length becomes 2. At that point, return whether the remaining two digits are the same. For s = "3902", the sequence becomes [3,9,0,2] -> [2,9,2] -> [1,1], so the answer is true.
Why this is enough here
The input size is small, so the repeated rebuild cost is acceptable. The real failure mode is not performance but applying the wrong transformation, such as summing full prefixes, forgetting modulo 10, or comparing too early before the string reaches length 2.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
If n is the length of s, the simulation does n - 2 rounds, and each round processes the current length minus one positions, so the total time is O(n^2). The extra space is O(n) if you build a fresh array for each round, which is completely fine for n <= 100.
What Interviewers Usually Probe
- The prompt says to perform the operation repeatedly as described, which strongly signals direct simulation instead of a hidden data structure trick.
- Math plus String is the right pattern because each new digit comes from local arithmetic on adjacent characters, not from parsing the whole number.
- An interviewer may watch whether you preserve the exact modulo 10 reduction on every pair, since that is the easiest place to misread the rule.
Common Pitfalls or Variants
Common pitfalls
- Using normal addition without modulo 10, which breaks cases where adjacent digits sum to 10 or more.
- Stopping after one pass or comparing the first two digits seen, instead of continuing until the string length is exactly 2.
- Building the next row in place from left to right without care, which can overwrite digits that are still needed for later adjacent sums in the same round.
Follow-up variants
- Return the final two-digit string instead of a boolean, which keeps the same repeated adjacent-sum simulation.
- Change the operation to multiply adjacent digits or use a different modulus, which keeps the shrinking process but changes the arithmetic rule.
- Ask for an optimized math derivation of the final digits using combinatorics, which is more relevant in a follow-up than in this Easy version.
FAQ
What is the simplest way to solve Check If Digits Are Equal in String After Operations I?
Use simulation. Repeatedly replace the string with adjacent digit sums modulo 10 until only two digits remain, then return whether those two digits are equal.
Why is Math plus String the right pattern for this problem?
Each step reads neighboring characters from a string-like sequence and applies small digit arithmetic. There is no need for greedy logic, dynamic programming, or graph processing.
Can this problem be solved in linear time?
For this Easy version, quadratic simulation is already sufficient because the string length is at most 100. There are math-based observations involving combinatorial weights, but they are unnecessary for the required constraints.
What causes wrong answers most often here?
The biggest mistakes are forgetting modulo 10, updating digits in place incorrectly, and stopping before the sequence has exactly two digits. Those errors usually fail small custom examples immediately.
How do I verify the example s = "3902"?
Start with digits 3, 9, 0, 2. The next row is 2, 9, 2 because 3 + 9 = 12, 9 + 0 = 9, and 0 + 2 = 2, all modulo 10; then 2 + 9 = 1 and 9 + 2 = 1, so the final two digits are equal.
Solution
Solution 1: Simulation
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.
class Solution:
def hasSameDigits(self, s: str) -> bool:
t = list(map(int, s))
n = len(t)
for k in range(n - 1, 1, -1):
for i in range(k):
t[i] = (t[i] + t[i + 1]) % 10
return t[0] == t[1]Continue Topic
math
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Math plus String
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward