Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Number of Strings Which Can Be Rearranged to Contain Substring

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2930" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given an integer n. A string s is called good if it contains only lowercase English characters and it is possible to rearrange the characters of s such that the new string contains "leet" as a substring. For example: The string "lteer" is good because we can rearrange it to form "leetr" . "letl" is not good because we cannot rearrange it to contain "leet" as a substring. Return the total number of good strings of length n. Since the answer may be large, return it modulo 109 + 7. A substring is a contiguous sequence of characters within a string. Example 1: Input: n = 4 Output: 12 Explanation: The 12 strings which can be rearranged to have "leet" as a substring are: "eelt", "eetl", "elet", "elte", "etel", "etle", "leet", "lete", "ltee", "teel", "tele", and "tlee". Example 2: Input: n = 10 Output: 83943898 Explanation: The number of strings with length 10 which can be rearranged to have "leet" as a substring is 526083947580. Hence the answer is 526083947580 % (109 + 7) = 83943898. Constraints: 1 <= n <= 105

Explanation

Here's the breakdown of the solution:

  • Inclusion-Exclusion Principle: Calculate the total number of strings of length n, then subtract the number of strings that don't contain "leet" as a substring. The complement approach is essential here.
  • Combinations with Repetition: The number of strings that don't contain "leet" is determined by the number of ways to choose characters while restricting the counts of 'l', 'e', and 't'. We use combinations with repetition to handle this, and precompute factorials and their inverses for efficiency.
  • Iterating through possible counts: Iterate through all the possible counts of 'l', 'e', and 't' that are less than the required counts in the "leet" substring. The inclusion-exclusion principle will alternate adding and subtracting these counts.

  • Runtime Complexity: O(n) for precomputation and O(n) for the main calculation, giving O(n) overall. Storage Complexity: O(n) to store factorials and their inverses.

Code

    def solve():
    n = int(input())
    MOD = 10**9 + 7
    fact = [1] * (n + 1)
    inv = [1] * (n + 1)
    for i in range(1, n + 1):
        fact[i] = (fact[i - 1] * i) % MOD

    inv[n] = pow(fact[n], MOD - 2, MOD)
    for i in range(n - 1, -1, -1):
        inv[i] = (inv[i + 1] * (i + 1)) % MOD

    def combinations(n, k):
        if k < 0 or k > n:
            return 0
        num = fact[n]
        den = (inv[k] * inv[n - k]) % MOD
        return (num * den) % MOD

    ans = pow(26, n, MOD)

    for l_count in range(2):
        for e_count in range(3):
            for t_count in range(2):
                if l_count + e_count + t_count == 0:
                    continue

                sign = 1 if (l_count + e_count + t_count) % 2 == 1 else -1

                remaining = n - (1 + e_count + t_count + l_count)
                if remaining < 0:
                    continue

                comb = combinations(n, 1+e_count+t_count+l_count)

                temp = (comb * pow(26,remaining,MOD)) % MOD
                ans = (ans - sign * temp) % MOD
                ans = (ans + MOD) % MOD # ensure positivity
    return ans % MOD
print(solve())

More from this blog

C

Chatmagic blog

2894 posts