Skip to content

2930. Number of Strings Which Can Be Rearranged to Contain Substring

  • Time: $O(\log n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
 public:
  int stringCount(int n) {
    // There're three invalid conditions:
    //   a. count('l') == 0
    //   b. count('e') < 2
    //   c. count('t') == 0
    //
    // By Principle of Inclusion-Exclusion (PIE):
    //   ans = allCount - a - b - c + ab + ac + bc - abc
    const long allCount = modPow(26, n);
    const long a = modPow(25, n);
    const long b = modPow(25, n);
    const long c = modPow(25, n) + n * modPow(25, n - 1);
    const long ab = modPow(24, n) + n * modPow(24, n - 1);
    const long ac = modPow(24, n);
    const long bc = modPow(24, n) + n * modPow(24, n - 1);
    const long abc = modPow(23, n) + n * modPow(23, n - 1);
    return ((allCount - a - b - c + ab + ac + bc - abc) % kMod + kMod) % kMod;
  }

 private:
  static constexpr int kMod = 1'000'000'007;

  long modPow(long x, long n) {
    if (n == 0)
      return 1;
    if (n % 2 == 1)
      return x * modPow(x % kMod, (n - 1)) % kMod;
    return modPow(x * x % kMod, (n / 2)) % kMod;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
  public int stringCount(int n) {
    // There're three invalid conditions:
    //   a. count('l') == 0
    //   b. count('e') < 2
    //   c. count('t') == 0
    //
    // By Principle of Inclusion-Exclusion (PIE):
    //   ans = allCount - a - b - c + ab + ac + bc - abc
    final long allCount = modPow(26, n);
    final long a = modPow(25, n);
    final long b = modPow(25, n);
    final long c = modPow(25, n) + n * modPow(25, n - 1);
    final long ab = modPow(24, n) + n * modPow(24, n - 1);
    final long ac = modPow(24, n);
    final long bc = modPow(24, n) + n * modPow(24, n - 1);
    final long abc = modPow(23, n) + n * modPow(23, n - 1);
    return (int) (((allCount - a - b - c + ab + ac + bc - abc) % kMod + kMod) % kMod);
  }

  private static final int kMod = 1_000_000_007;

  private long modPow(long x, long n) {
    if (n == 0)
      return 1;
    if (n % 2 == 1)
      return x * modPow(x, n - 1) % kMod;
    return modPow(x * x % kMod, n / 2);
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution:
  def stringCount(self, n: int) -> int:
    # There're three invalid conditions:
    #   a. count('l') == 0
    #   b. count('e') < 2
    #   c. count('t') == 0
    #
    # By Principle of Inclusion-Exclusion (PIE):
    #   ans = allCount - a - b - c + ab + ac + bc - abc
    kMod = 1_000_000_007
    allCount = pow(26, n, kMod)
    a = pow(25, n, kMod)
    b = pow(25, n, kMod)
    c = pow(25, n, kMod) + n * pow(25, n - 1, kMod)
    ab = pow(24, n, kMod) + n * pow(24, n - 1, kMod)
    ac = pow(24, n, kMod)
    bc = pow(24, n, kMod) + n * pow(24, n - 1, kMod)
    abc = pow(23, n, kMod) + n * pow(23, n - 1, kMod)
    return (allCount - a - b - c + ab + ac + bc - abc) % kMod