Watch 7 video solutions for Check If Digits Are Equal in String After Operations II, a hard level problem involving Math, String, Combinatorics. This walkthrough by Aryan Mittal has 6,290 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a string s consisting of digits. Perform the following operation repeatedly until the string has exactly two digits:
s, starting from the first digit, calculate a new digit as the sum of the two digits modulo 10.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:
s = "3902"(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 = 2s becomes "292"(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1s becomes "11""11" are the same, the output is true.Example 2:
Input: s = "34789"
Output: false
Explanation:
s = "34789".s = "7157".s = "862".s = "48".'4' != '8', the output is false.
Constraints:
3 <= s.length <= 105s consists of only digits.Problem Overview: You start with a numeric string. In each operation, replace the string with a new one where every position becomes (s[i] + s[i+1]) % 10. Repeat until only two digits remain. The task is to check whether those final two digits are equal.
Approach 1: Direct Simulation (Brute Force) (Time: O(n^2), Space: O(n))
The most straightforward solution literally simulates the process. Build the next string by iterating through adjacent pairs and computing (digit[i] + digit[i+1]) % 10. Each operation reduces the length by one, so a string of length n requires n-2 rounds. Since each round processes almost the entire string, the total work becomes quadratic. This approach works for small inputs and helps verify the transformation pattern, but it quickly becomes too slow for large n.
Approach 2: Combinatorics with Binomial Coefficients mod 10 (Optimal) (Time: O(n), Space: O(1))
The repeated pair-sum operation forms a structure identical to Pascal's Triangle. After k reductions, each digit becomes a weighted sum of original digits using binomial coefficients. After n-2 operations, the final two digits are linear combinations of the original digits:
A = sum(C(n-2, i) * s[i]) mod 10
B = sum(C(n-2, i) * s[i+1]) mod 10
The problem reduces to computing binomial coefficients C(n-2, i) modulo 10. Since 10 is not prime, standard modular combinatorics does not apply directly. Split the computation into mod 2 and mod 5 using Lucas' theorem, then combine the results with the Chinese Remainder principle. Iterate once through the string, compute each coefficient modulo 10, and accumulate the two sums.
This transforms the problem from repeatedly rebuilding strings into a single linear pass with combinatorial weights. The approach relies heavily on concepts from combinatorics, number theory, and careful modular arithmetic over a math-driven observation.
Recommended for interviews: Start by explaining the brute force simulation because it demonstrates how the operation works. Then derive the Pascal triangle pattern and move to the combinatorial formulation. Interviewers expect the optimized binomial-coefficient solution since it reduces the complexity from O(n^2) to O(n) and shows strong pattern recognition and modular arithmetic skills.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Simulation | O(n^2) | O(n) | Understanding the transformation process or when input size is very small |
| Combinatorics with Binomial Coefficients mod 10 | O(n) | O(1) | Large inputs where repeated simulation is too slow; optimal interview solution |