Sam and substrings Discussions | Algorithms | HackerRank
  • + 0 comments

    js

    const mod = BigInt(10 ** 9 + 7);
    const zero = BigInt(0);
    const one = BigInt(1);
    const ten = BigInt(10);
    function substrings(n) {
        const len = BigInt(n.length);
        let sum = zero;
        let f1 = one;
        let f2 = ten;
        for (let i = len - one; i >= zero; i--) {
            [sum, f1, f2] = [
                (sum + BigInt(n[i]) * (i + one) * f1) % mod,
                (f1 + f2) % mod,
                (f2 * ten) % mod
            ];
        }
    
        return sum;
    }