Check If Digits Are Equal in String After Operations I - Problem

You are given a string s consisting of digits. Your task is to simulate a digit reduction process that repeatedly combines adjacent digits until only two digits remain.

The Process:

  • For each pair of consecutive digits in s, starting from the first digit, calculate a new digit as (digit1 + digit2) % 10
  • Replace the entire string with these newly calculated digits
  • Repeat until exactly two digits remain

Goal: Return true if the final two digits are identical, false otherwise.

Example: "123""15" (1+2=3, 2+3=5) → Since 1 ≠ 5, return false

Input & Output

example_1.py — Basic Case
$ Input: s = "1234"
Output: false
💡 Note: Step 1: "1234" → "357" (1+2=3, 2+3=5, 3+4=7). Step 2: "357" → "82" (3+5=8, 5+7=12→2). Final digits 8 and 2 are different, so return false.
example_2.py — Equal Result
$ Input: s = "12"
Output: true
💡 Note: String already has exactly 2 digits. Since we need exactly 2 digits to stop, and 1 ≠ 2, this would be false. Wait, let me recalculate: if we have "12", we stop immediately and check 1 == 2, which is false.
example_3.py — Single Reduction
$ Input: s = "999"
Output: true
💡 Note: Step 1: "999" → "88" (9+9=18→8, 9+9=18→8). Final digits are both 8, so return true.

Visualization

Tap to expand
Complete Digit Reduction ProcessRound 1: "12345"12345Round 2: "3579"3579Round 3: "826"826Final: "08"081+2=3, 2+3=5, 3+4=7, 4+5=93+5=8, 5+7=12→2, 7+9=16→68+2=10→0, 2+6=80 ≠ 8 → false
Understanding the Visualization
1
Parse Input
Convert string digits to integer array for easier manipulation
2
Iterative Reduction
While more than 2 digits exist, combine adjacent pairs using modulo 10
3
Create New Sequence
Each iteration produces a sequence one element shorter
4
Final Comparison
Compare the last two remaining digits for equality
Key Takeaway
🎯 Key Insight: This problem requires faithful simulation - there are no mathematical shortcuts to skip the iterative reduction process.

Time & Space Complexity

Time Complexity
⏱️
O(n²)

In worst case, we process n + (n-1) + (n-2) + ... + 2 = O(n²) digit operations

n
2n
Quadratic Growth
Space Complexity
O(n)

We store intermediate results in arrays/strings of decreasing size

n
2n
Linearithmic Space

Constraints

  • 2 ≤ s.length ≤ 1000
  • s consists of digits only
  • s contains at least 2 digits initially
Asked in
Google 25 Amazon 18 Meta 15 Microsoft 12
23.3K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen