Count of Distinct strings possible by inserting K characters in the original string
Last Updated :
15 Jul, 2025
Given a string S and an integer K, the task is to find the total number of strings that can be formed by inserting exactly K characters at any position of the string S. Since the answer can be large, print it modulo 109+7.
Examples:
Input: S = "a" K = 1
Output: 51
Explanation:
Since any of the 26 characters can be inserted at before 'a' or after 'a', a total of 52 possible strings can be formed.
But the string "aa" gets formed twice. Hence count of distinct strings possible is 51.
Input: S = "abc" K = 2
Output: 6376
Approach:
The idea is to find the number of strings that contains the str as a subsequence. Follow the steps below to solve the problem:
- The total number of strings that can be formed by N characters is 26N.
- Calculate 26N using Binary Exponentiation.
- In this problem, only the strings that contain the str as a subsequence needs to be considered.
- Hence, the final count of strings is given by
(total number of strings) - (number of strings that don't contain the input string as a sub-sequence)
- While calculating such strings that don't contain the str as a subsequence, observe that the length of the prefix of S is a subsequence of the resulting string can be between 0 to |S|-1.
- For every prefix length from 0 to |S|-1, find the total number of strings that can be formed with such a prefix as a sub-sequence. Then subtract that value from 26N.
- Hence, the final answer is:
26^{N} - \sum_{i = 0}^{|S| - 1} \binom{N}{i}*25^{N - i}
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
#define int long long int
const int mod = 1e9 + 7;
// Function to calculate and return
// x^n in log(n) time using
// Binary Exponentiation
int binExp(int base, int power)
{
int x = 1;
while (power) {
if (power % 2 == 1)
x = ((x % mod)
* (base % mod))
% mod;
base = ((base % mod)
* (base % mod))
% mod;
power = power / 2;
}
return x;
}
// Function to calculate the factorial
// of a number
int fact(int num)
{
int result = 1;
for (int i = 1; i <= num; ++i) {
result = ((result % mod)
* (i % mod))
% mod;
}
return result;
}
// Function to calculate combination
int calculate_nCi(int N, int i)
{
// nCi = ( n! ) / (( n-i )! * i!)
int nfact = fact(N);
int ifact = fact(i);
int dfact = fact(N - i);
// Using Euler's theorem of Modular
// multiplicative inverse to find
// the inverse of a number.
// (1/a)%mod=a^(m?2)%mod
int inv_ifact = binExp(ifact, mod - 2);
int inv_dfact = binExp(dfact, mod - 2);
int denm
= ((inv_ifact % mod)
* (inv_dfact % mod))
% mod;
int answer = ((nfact % mod)
* (denm % mod))
% mod;
return answer;
}
// Function to find the count of
// possible strings
void countSubstring(int N, int s, int k)
{
// Number of ways to form all
// possible strings
int allWays = binExp(26, N);
// Number of ways to form strings
// that don't contain the input
// string as a subsequence
int noWays = 0;
// Checking for all prefix length
// from 0 to |S|-1.
for (int i = 0; i < s; ++i) {
// to calculate nCi
int nCi = calculate_nCi(N, i);
// Select the remaining characters
// 25 ^ (N-i)
int remaining = binExp(25, N - i);
int multiply
= ((nCi % mod)
* (remaining % mod))
% mod;
// Add the answer for this prefix
// length to the final answer
noWays = ((noWays % mod)
+ (multiply % mod))
% mod;
}
// Answer is the difference of
// allWays and noWays
int answer = ((allWays % mod)
- (noWays % mod))
% mod;
if (answer < 0)
answer += mod;
// Print the answer
cout << answer;
}
// Driver Code
int32_t main()
{
string str = "abc";
int k = 2;
int s = str.length();
int N = s + k;
countSubstring(N, s, k);
}
Java
// Java program for the above approach
class GFG{
static final long mod = 1000000007;
// Function to calculate and return
// x^n in log(n) time using
// Binary Exponentiation
static long binExp(long base, long power)
{
long x = 1;
while (power != 0)
{
if (power % 2 == 1)
x = ((x % mod) *
(base % mod)) % mod;
base = ((base % mod) *
(base % mod)) % mod;
power = power / 2;
}
return x;
}
// Function to calculate the factorial
// of a number
static long fact(long num)
{
long result = 1;
for(long i = 1; i <= num; ++i)
{
result = ((result % mod) *
(i % mod)) % mod;
}
return result;
}
// Function to calculate combination
static long calculate_nCi(long N, long i)
{
// nCi = ( n! ) / (( n-i )! * i!)
long nfact = fact(N);
long ifact = fact(i);
long dfact = fact(N - i);
// Using Euler's theorem of Modular
// multiplicative inverse to find
// the inverse of a number.
// (1/a)%mod=a^(m?2)%mod
long inv_ifact = binExp(ifact, mod - 2);
long inv_dfact = binExp(dfact, mod - 2);
long denm = ((inv_ifact % mod) *
(inv_dfact % mod)) % mod;
long answer = ((nfact % mod) *
(denm % mod)) % mod;
return answer;
}
// Function to find the count of
// possible strings
static void countSubstring(long N, long s, long k)
{
// Number of ways to form all
// possible strings
long allWays = binExp(26, N);
// Number of ways to form strings
// that don't contain the input
// string as a subsequence
long noWays = 0;
// Checking for all prefix length
// from 0 to |S|-1.
for(long i = 0; i < s; ++i)
{
// To calculate nCi
long nCi = calculate_nCi(N, i);
// Select the remaining characters
// 25 ^ (N-i)
long remaining = binExp(25, N - i);
long multiply = ((nCi % mod) *
(remaining % mod)) % mod;
// Add the answer for this prefix
// length to the final answer
noWays = ((noWays % mod) +
(multiply % mod)) % mod;
}
// Answer is the difference of
// allWays and noWays
long answer = ((allWays % mod) -
(noWays % mod)) % mod;
if (answer < 0)
answer += mod;
// Print the answer
System.out.println(answer);
}
// Driver code
public static void main(String[] args)
{
String str = "abc";
long k = 2;
long s = str.length();
long N = s + k;
countSubstring(N, s, k);
}
}
// This code is contributed by rutvik_56
Python3
# Python3 program for the above approach
mod = 1000000007
# Function to calculate and return
# x^n in log(n) time using
# Binary Exponentiation
def binExp(base, power):
x = 1
while (power):
if (power % 2 == 1):
x = (((x % mod) *
(base % mod)) % mod)
base = (((base % mod) *
(base % mod)) % mod)
power = power // 2
return x
# Function to calculate the factorial
# of a number
def fact(num):
result = 1
for i in range(1, num + 1):
result = (((result % mod) *
(i % mod)) % mod)
return result
# Function to calculate combination
def calculate_nCi(N, i):
# nCi = ( n! ) / (( n-i )! * i!)
nfact = fact(N)
ifact = fact(i)
dfact = fact(N - i)
# Using Euler's theorem of Modular
# multiplicative inverse to find
# the inverse of a number.
# (1/a)%mod=a^(m?2)%mod
inv_ifact = binExp(ifact, mod - 2)
inv_dfact = binExp(dfact, mod - 2)
denm = (((inv_ifact % mod) *
(inv_dfact % mod)) % mod)
answer = (((nfact % mod) *
(denm % mod)) % mod)
return answer
# Function to find the count of
# possible strings
def countSubstring(N, s, k):
# Number of ways to form all
# possible strings
allWays = binExp(26, N)
# Number of ways to form strings
# that don't contain the input
# string as a subsequence
noWays = 0
# Checking for all prefix length
# from 0 to |S|-1.
for i in range(s):
# To calculate nCi
nCi = calculate_nCi(N, i)
# Select the remaining characters
# 25 ^ (N-i)
remaining = binExp(25, N - i)
multiply = (((nCi % mod) *
(remaining % mod)) % mod)
# Add the answer for this prefix
# length to the final answer
noWays =(((noWays % mod) +
(multiply % mod)) % mod)
# Answer is the difference of
# allWays and noWays
answer = (((allWays % mod) -
(noWays % mod)) % mod)
if (answer < 0):
answer += mod
# Print the answer
print(answer)
# Driver Code
if __name__ == "__main__":
st = "abc"
k = 2
s = len(st)
N = s + k
countSubstring(N, s, k)
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
class GFG{
static readonly long mod = 1000000007;
// Function to calculate and return
// x^n in log(n) time using
// Binary Exponentiation
static long binExp(long Base, long power)
{
long x = 1;
while (power != 0)
{
if (power % 2 == 1)
x = ((x % mod) *
(Base % mod)) % mod;
Base = ((Base % mod) *
(Base % mod)) % mod;
power = power / 2;
}
return x;
}
// Function to calculate the factorial
// of a number
static long fact(long num)
{
long result = 1;
for(long i = 1; i <= num; ++i)
{
result = ((result % mod) *
(i % mod)) % mod;
}
return result;
}
// Function to calculate combination
static long calculate_nCi(long N, long i)
{
// nCi = ( n! ) / (( n-i )! * i!)
long nfact = fact(N);
long ifact = fact(i);
long dfact = fact(N - i);
// Using Euler's theorem of Modular
// multiplicative inverse to find
// the inverse of a number.
// (1/a)%mod=a^(m?2)%mod
long inv_ifact = binExp(ifact, mod - 2);
long inv_dfact = binExp(dfact, mod - 2);
long denm = ((inv_ifact % mod) *
(inv_dfact % mod)) % mod;
long answer = ((nfact % mod) *
(denm % mod)) % mod;
return answer;
}
// Function to find the count of
// possible strings
static void countSubstring(long N, long s, long k)
{
// Number of ways to form all
// possible strings
long allWays = binExp(26, N);
// Number of ways to form strings
// that don't contain the input
// string as a subsequence
long noWays = 0;
// Checking for all prefix length
// from 0 to |S|-1.
for(long i = 0; i < s; ++i)
{
// To calculate nCi
long nCi = calculate_nCi(N, i);
// Select the remaining characters
// 25 ^ (N-i)
long remaining = binExp(25, N - i);
long multiply = ((nCi % mod) *
(remaining % mod)) % mod;
// Add the answer for this prefix
// length to the readonly answer
noWays = ((noWays % mod) +
(multiply % mod)) % mod;
}
// Answer is the difference of
// allWays and noWays
long answer = ((allWays % mod) -
(noWays % mod)) % mod;
if (answer < 0)
answer += mod;
// Print the answer
Console.WriteLine(answer);
}
// Driver code
public static void Main(String[] args)
{
String str = "abc";
long k = 2;
long s = str.Length;
long N = s + k;
countSubstring(N, s, k);
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// JavaScript program for the above approach
var mod = 10007;
// Function to calculate and return
// x^n in log(n) time using
// Binary Exponentiation
function binExp(base , power) {
var x = 1;
while (power != 0) {
if (power % 2 == 1)
x = (((x % mod) * (base % mod)) % mod);
base = (((base % mod) * (base % mod)) % mod);
power = parseInt(power / 2);
}
return x;
}
// Function to calculate the factorial
// of a number
function fact(num) {
var result = 1;
for (var i = 1; i <= num; ++i) {
result = (((result % mod) * (i % mod)) % mod);
}
return result;
}
// Function to calculate combination
function calculate_nCi(N , i) {
// nCi = ( n! ) / (( n-i )! * i!)
var nfact = fact(N);
var ifact = fact(i);
var dfact = fact(N - i);
// Using Euler's theorem of Modular
// multiplicative inverse to find
// the inverse of a number.
// (1/a)%mod=a^(m?2)%mod
var inv_ifact = binExp(ifact, mod - 2);
var inv_dfact = binExp(dfact, mod - 2);
var denm = (((inv_ifact % mod) * (inv_dfact % mod)) % mod);
var answer = (((nfact % mod) * (denm % mod)) % mod);
return answer;
}
// Function to find the count of
// possible strings
function countSubstring(N , s , k) {
// Number of ways to form all
// possible strings
var allWays = binExp(26, N);
// Number of ways to form strings
// that don't contain the input
// string as a subsequence
var noWays = 0;
// Checking for all prefix length
// from 0 to |S|-1.
for (var i = 0; i < s; ++i) {
// To calculate nCi
var nCi = calculate_nCi(N, i);
// Select the remaining characters
// 25 ^ (N-i)
var remaining = binExp(25, N - i);
var multiply = (((nCi % mod) * (remaining % mod)) % mod);
// Add the answer for this prefix
// length to the final answer
noWays = (((noWays % mod) + (multiply % mod)) % mod);
}
// Answer is the difference of
// allWays and noWays
var answer = (((allWays % mod) - (noWays % mod)) % mod);
if (answer < 0)
answer += mod;
// Print the answer
document.write(answer);
}
// Driver code
var str = "abc";
var k = 2;
var s = str.length;
var N = s + k;
countSubstring(N, s, k);
// This code contributed by umadevi9616
</script>
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1)
Similar Reads
Euler Totient for Competitive Programming What is Euler Totient function(ETF)?Euler Totient Function or Phi-function for 'n', gives the count of integers in range '1' to 'n' that are co-prime to 'n'. It is denoted by \phi(n) .For example the below table shows the ETF value of first 15 positive integers: 3 Important Properties of Euler Totie
8 min read
Euler's Totient Function Given an integer n, find the value of Euler's Totient Function, denoted as Φ(n). The function Φ(n) represents the count of positive integers less than or equal to n that are relatively prime to n. Euler's Totient function Φ(n) for an input n is the count of numbers in {1, 2, 3, ..., n-1} that are re
10 min read
Count of non co-prime pairs from the range [1, arr[i]] for every array element Given an array arr[] consisting of N integers, the task for every ith element of the array is to find the number of non co-prime pairs from the range [1, arr[i]]. Examples: Input: N = 2, arr[] = {3, 4}Output: 2 4Explanation: All non-co-prime pairs from the range [1, 3] are (2, 2) and (3, 3).All non-
13 min read
Generate an array having sum of Euler Totient Function of all elements equal to N Given a positive integer N, the task is to generate an array such that the sum of the Euler Totient Function of each element is equal to N. Examples: Input: N = 6Output: 1 6 2 3 Input: N = 12Output: 1 12 2 6 3 4 Approach: The given problem can be solved based on the divisor sum property of the Euler
5 min read
Count all possible values of K less than Y such that GCD(X, Y) = GCD(X+K, Y) Given two integers X and Y, the task is to find the number of integers, K, such that gcd(X, Y) is equal to gcd(X+K, Y), where 0 < K <Y. Examples: Input: X = 3, Y = 15Output: 4Explanation: All possible values of K are {0, 3, 6, 9} for which GCD(X, Y) = GCD(X + K, Y). Input: X = 2, Y = 12Output:
8 min read
Count of integers up to N which are non divisors and non coprime with N Given an integer N, the task is to find the count of all possible integers less than N satisfying the following properties: The number is not coprime with N i.e their GCD is greater than 1.The number is not a divisor of N. Examples: Input: N = 10 Output: 3 Explanation: All possible integers which ar
5 min read
Find the number of primitive roots modulo prime Given a prime p . The task is to count all the primitive roots of p .A primitive root is an integer x (1 <= x < p) such that none of the integers x - 1, x2 - 1, ...., xp - 2 - 1 are divisible by p but xp - 1 - 1 is divisible by p . Examples: Input: P = 3 Output: 1 The only primitive root modul
5 min read
Compute power of power k times % m Given x, k and m. Compute (xxxx...k)%m, x is in power k times. Given x is always prime and m is greater than x. Examples: Input : 2 3 3 Output : 1 Explanation : ((2 ^ 2) ^ 2) % 3 = (4 ^ 2) % 3 = 1 Input : 3 2 3 Output : 0 Explanation : (3^3)%3 = 0 A naive approach is to compute the power of x k time
15+ min read
Primitive root of a prime number n modulo n Given a prime number n, the task is to find its primitive root under modulo n. The primitive root of a prime number n is an integer r between[1, n-1] such that the values of r^x(mod n) where x is in the range[0, n-2] are different. Return -1 if n is a non-prime number. Examples: Input : 7 Output : S
15 min read
Euler's Totient function for all numbers smaller than or equal to n Euler's Totient function ?(n) for an input n is the count of numbers in {1, 2, 3, ..., n} that are relatively prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1. For example, ?(4) = 2, ?(3) = 2 and ?(5) = 4. There are 2 numbers smaller or equal to 4 that are relatively pri
13 min read