NOT Luca's theorem ahh solution✅Both 100%
Complexity :
Time complexity:O(n)

Space complexity:O(n)
Code :
![image.png](https://assets.leetcode.com/users/images/5504f873-0176-4907-9d9b-ec73703412e1_1740332367.604966.png)

----

# Complexity :
- Time complexity: $O(n)$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $O(n)$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
---
# Code :
```cpp []

class Solution {
    int invMod10[10];
    int pow2mod10(long long p) {
        switch (p % 4) {
            case 0: return 6;
            case 1: return 2;
            case 2: return 4;
            default: return 8;
        }
    }
    void factor(int n, int &c2, int &c5, int &r) {
        c2 = 0; c5 = 0;
        while (n and n % 2 == 0) { n /= 2; c2++; }
        while (n and n % 5 == 0) { n /= 5; c5++; }
        r = n % 10;
    }
    int getFinalValue(long long c2, long long c5, int v) {
        if (c2 > 0 && c5 > 0) return 0;
        int ans = v;
        if (c2 > 0) ans = (ans * pow2mod10(c2)) % 10;
        if (c5 > 0) ans = (ans * 5) % 10;
        return ans;
    }

    vector<int> getPascalsTriangleRowMod10(int x) {
        for(int i=0;i<10;i++) invMod10[i] = -1;
        invMod10[1] = 1; invMod10[3] = 7; invMod10[7] = 3; invMod10[9] = 9;
        vector<int> res(x+1);
        res[0] = 1;
        long long c2 = 0, c5 = 0;
        int val = 1;
        for(int k=1; k<=x; k++){
            int a2,a5,rem; factor(x - k + 1, a2, a5, rem);
            c2 += a2; c5 += a5; val = (val * rem) % 10;
            int b2,b5,brem; factor(k, b2, b5, brem);
            c2 -= b2; c5 -= b5; val = (val * invMod10[brem]) % 10;
            res[k] = getFinalValue(c2, c5, val);
        }
        return res;
    }
public:
    bool hasSameDigits(string s) {
        vector<int> p = getPascalsTriangleRowMod10(s.length() - 2);
        int res = 0;
        for(int i = 0; i < p.size(); i++) {
            res = (res + (s[i] - s[i+1]) * p[i] % 10) % 10;
        }

        return res % 10 == 0;
    }
};
```
['C++']