Two Digits Equivalence via Binomial Coefficients
Intuition: Instead of simulating all operations until the string is reduced to two digits (which would be too slow for large inputs), we observe that each of th
**Intuition**: Instead of simulating all operations until the string is reduced to two digits (which would be too slow for large inputs), we observe that each of the final two digits is essentially a weighted sum of the original digits. The weights are given by binomial coefficients (specifically, binom(n-2, i) mod 10). This lets us compute the result directly without simulation.

**Approach**: We break down the computation of binom(n-2, i) modulo 10 into two parts because 10 = 2 * 5. For modulo 2, we use the fact that binom(n, k) mod 2 is 1 if every set bit in k is also set in n. For modulo 5, we apply Lucas’ Theorem to compute the coefficients based on the base-5 representation of the numbers. Then, we combine the results (using a precomputed mapping) to get the binomial coefficients modulo 10. Finally, we compute the weighted sums for the two final digits and check if they are equal.

**Complexity**:
                                                            **Time complexity**: O(n log n) (each coefficient computation involves decomposing numbers in base 5, which contributes a logarithmic factor) 
                                                            **Space complexity:** O(n) (we only use a few extra arrays and variables in addition to the input) 

# Code
```python []
class Solution(object):
    def hasSameDigits(self, s):
        n = len(s)
        if n < 3:
            return False
        A = [ord(c) - 48 for c in s]
        k = n - 2
        fact = [None] * (k + 1)
        for i in range(1, k + 1):
            x = i
            cnt = 0
            while x % 5 == 0:
                cnt += 1
                x //= 5
            fact[i] = (cnt, x % 5)
        inv = {1: 1, 2: 3, 3: 2, 4: 4}
        D2 = 0
        D5 = 0
        v = 0
        r_val = 1
        for j in range(k):
            c2 = 1 if (k & j) == j else 0
            d = A[j + 1] - A[j]
            d2 = d % 2
            d5 = d % 5
            D2 = (D2 + c2 * d2) % 2
            coeff5 = 0 if v > 0 else r_val
            D5 = (D5 + coeff5 * d5) % 5
            num = k - j
            den = j + 1
            cnt_num, rem_num = fact[num]
            cnt_den, rem_den = fact[den]
            v += cnt_num - cnt_den
            r_val = (r_val * rem_num * inv[rem_den]) % 5
        c2 = 1
        d = A[k + 1] - A[k]
        d2 = d % 2
        d5 = d % 5
        D2 = (D2 + c2 * d2) % 2
        coeff5 = 0 if v > 0 else r_val
        D5 = (D5 + coeff5 * d5) % 5
        return D2 == 0 and D5 == 0

```
['Python']