You're given a string s containing only digits. Your task is to repeatedly perform a digit compression operation until exactly two digits remain.
The Operation: For each pair of consecutive digits in the string, calculate their sum modulo 10. Replace the entire string with these newly computed digits, maintaining their order.
For example: "1234" becomes "357" because:
1 + 2 = 32 + 3 = 53 + 4 = 7
Continue this process until only two digits remain. Return true if these final two digits are identical, otherwise return false.
This problem connects to Pascal's Triangle and combinatorics - each final digit is actually a weighted sum of the original digits!
Input & Output
Visualization
Time & Space Complexity
Each iteration processes n-1 pairs, and we have n-1 iterations, giving us O(nยฒ) total operations
We create new strings at each level, but only store the current level
Constraints
- 2 โค s.length โค 1000
- s consists of digits only
- The input string will always have at least 2 characters