Math alternative to  Inclusion-Exclusion
Intuition\nThis solution provides two implementation and another way to come up with the formula given by the inclusion and exclusion. One approach runs slower 
# Intuition\nThis solution provides two implementation and another way to come up with the formula given by the inclusion and exclusion. One approach runs slower than the solution by inclusion/exculsion but the solution provides some interesting mathematical insights which I discovered on the way and so why not to share it. The other is just the standard implementation of the inclusion and exclusion principle.\n\n# Approach\nThis solution tackles the problem by breaking it down into two main parts: counting the number of ways to fill certain positions with any characters except \'l\', \'e\', \'t\', and then calculating the number of valid strings that contain only the letters \'e\', \'l\', \'t\', under specific constraints. The final result integrates these parts using combinatorial mathematics.\n\n## Part 1 General Formula\n\n$$ TotalAmount = \\sum_{k=0}^{N-4} 23^k \\binom{n}{k} d_{N-k} $$\n* there $23^k$ ways to fill in with any character except for `e`, `l`, `t` and since we can choose any k slots, it is accounted by $\\binom{n}{k} $\n* `d_L` represents the number of valid strings of length \n`L` that consist only of the letters \'e\', \'l\', \'t\', and satisfy the requirement of containing at least two \'e\'s, one \'t\', and  \'l\'s in the remaining positions\n\n## Part 2 Deriving $d_L$\n\n$d_L$ represents the number of valid strings of length `L` that consist only of the letters `e`, `l`, `t`, and satisfy the requirement of containing at least two `e` s, one `t`, at least one `l`s in the remaining positions. The derivation of \n\n\u200B$d_L = \\sum_{x=3}^{L-1} \\binom{L}{x} \\sum_{t=1}^{x-2} \\binom{x}{t} = \\sum_{x=3}^{L-1} \\binom{L}{x} \\left[2^x - 2 - x\\right] = \\sum_{x=0}^{L} \\binom{L}{x} \\left[2^x - 2 - x\\right] - \\sum_{x=0}^{2} \\binom{L}{x} \\left[2^x - 2 - x\\right] - \\binom{L}{L} \\left[2^L - 2 - L\\right]  = 3^L - 2^{L+1} + 3 + 2L - (2^L - 3 - 2L) =\n$\nIn more detail\n* $$\\sum_{x=0}^{L} \\binom{L}{x} 2^x = 3^L$$\n* $$\\sum_{x=0}^{L} \\binom{L}{x} 2 = 2^{L+1}$$\n* $$\\sum_{x=0}^{L} \\binom{L}{x} x = \\sum_{x=0}^{L} \\binom{L-1}{x-1} L =  L \\cdot 2^{L-1}$$\n\nSo the final formula that is implemented in the first code is\n$$\nT = \\sum_{k=0}^{N-4} 23^k \\binom{n}{k} \\left(3^{N-k} - 2^{N-k-1} \\cdot (6+N-k) + 3+2\\cdot (N-k) \\right)\n$$\n\nOf course this can be further simplified:\n$$\nT = \\sum_{k=4}^{N} 23^{n-k} \\binom{n}{k} \\left(3^{k} - 2^{k-1} \\cdot (6+k) + 3+2\\cdot k \\right) = \\sum_{k=0}^{N} 23^{n-k} \\binom{n}{k} \\left(3^{k} - 2^{k-1} \\cdot (6+k) + 3+2\\cdot k \\right) - \\sum_{k=0}^{3} 23^{n-k} \\binom{n}{k} \\left(3^{k} - 2^{k-1} \\cdot (6+k) + 3+2\\cdot k \\right) = \\sum_{k=0}^{N} 23^{n-k} \\binom{n}{k} \\left(3^{k} - 2^{k-1} \\cdot (6+k) + 3+2\\cdot k \\right) -23^{n-1} *(n+23)\n$$ \nNow let\'s look at the first sum\n$$\n \\sum_{k=0}^{N} 23^{n-k} \\binom{n}{k} \\left(3^{k} - 2^{k-1} \\cdot (6+k) + 3+2\\cdot k \\right) =  \\sum_{k=0}^{N} 23^{n-k} \\binom{n}{k} \\left(3^{k} - 2^{k} 3 + 3 \\right) +  \\sum_{k=0}^{N} 23^{n-k} \\binom{n}{k} k \\left(- 2^{k-1} + 2 \\right) = \n \\sum_{k=0}^{N} 23^{n-k} \\binom{n}{k} \\left(3^{k} - 2^{k} 3 + 3 \\right) +  \\sum_{k=0}^{N} 23^{n-k} \\binom{n-1}{k-1} n \\left(- 2^{k-1} + 2 \\right)\n= 26^{n-1}-3\\cdot(25^{n}-24^n) + n\\cdot(-25^{n-1}+2\\cdot 24^{n-1})\n= 26^{n-1} -25^{n-1} (75+n)+24^{n-1}(72+2n)\n$$\n\nFinally,\n$$\nT = \\sum_{k=0}^{N} 23^{n-k} \\binom{n}{k} \\left(3^{k} - 2^{k-1} \\cdot (6+k) + 3+2\\cdot k \\right) -23^{n-1} *(n+23) = 26^{n-1} -25^{n-1} (75+n)+24^{n-1}(72+2n) - 23^{n-1} *(n+23)\n$$\n\nWhich is another way of arriving to the result of the inclusion exclusion. Makes you kind of appreaciate the method right :D\n# Complexity\n- Time complexity:\n$$O(n)$$ \n\n- Space complexity:\n$$O(1)$$ \n\n# Code\n```\nclass Solution:\n    def stringCount(self, n: int) -> int:\n    ## The explicit formula\n    ##     T = \\sum_{k=0}^{n-4} 23^k * (3^n - 2^{n-1} * (6 + n - k) + 3 + ##  2 *(n - k)) \\binom{n}{k} \n\n\n        mod = 10**9 + 7\n        total = 0\n        twentythrees = 1\n        threes = pow(3, n, mod)\n        twos = pow(2, n-1, mod)\n        comb_nk = 1  # Initial value for comb(n, 0)\n        #Precompute modular inverses for 3 and 2 to use for division in modular arithmetic, \n#this is true by Fermat\'s little theorem\n        x1 = pow(3, mod-2, mod)\n        x2 = pow(2, mod-2, mod)\n        for k in range(n-3):\n            total += twentythrees * (threes - twos * (6 + n - k) + 3 + 2 * (n - k)) * comb_nk % mod\n            total %= mod\n            \n            # Update twentythrees, threes, and twos for the next iteration\n            twentythrees = twentythrees * 23 % mod\n            threes = threes * x1 % mod  # Modular inverse for division\n            twos = twos * x2 % mod\n\n            # Update comb_nk for the next k value using the property of binomial coefficients\n            comb_nk = comb_nk * (n - k) * pow(k + 1, mod-2, mod) % mod\n        \n        return total\n```\n```\nclass Solution:\n    def stringCount(self, n: int) -> int:\n## the expliit formula        \n## \\sum_{k=0}^{n-4} 23^k * (3^n - 2^{n-1} * (6 + n - k) + 3 + 2 *(n - k)) \\binom{n}{k} \n\n        mod = 10**9 + 7\n        invalidStrings = (\n            pow(25, n-1, mod) *((n+75) % mod)\n            - pow(24, n-1, mod) *((72+2*n) % mod)\n            + pow(23, n-1, mod) *((n+23) % mod)\n        ) % mod\n        allStrings = pow(26,n,mod)\n        return  (allStrings - invalidStrings ) % mod\n        \n```\n\n
['Math', 'Python3']