Counting K-Length Strings with Fixed Character in a Unique String
Last Updated :
08 Mar, 2024
Given a string S of length n containing distinct characters and a character C , the task is to count k-length strings that can be formed using characters from the string S, ensuring each string includes the specified character C, and no characters from the given string S are used more than once. Return the answer by taking modulo of 1e9 + 7.
Example:
Input: C = ‘a’, S = “abc”, k = 2
Output: 4
Explanation: All two-length strings are: {ab, ac, ba, bc, ca, cb}
All valid strings including character ‘C’ are: {ab, ac, ba, ca}
Input: C = ‘c’, S = “abcde”, k = 3
Output: 36
Approach:
Think about complement approach that is: Count of total two length strings – Count of two length string that doesn’t containing given character C.
Formula: nCk * k! – (n – 1)Ck * k!
- nCk * k!: Select k elements from n characters (i.e, nCk) and permulte them (i.e, k!)
- (n – 1)Ck * k!: Assume given character is not present in S then selecting k elements from (n – 1) (i.e, (n-1)Ck ) and permute them (k!)
Steps-by-step approach:
- Initialize an array fact to store factorials.
- Calculate factorials up to M using a loop.
- Calculate a^b under modulus mod using binary exponentiation.
- Calculate the modular multiplicative inverse of a under modulus mod.
- Calculate “n choose r” (combination) under modulus mod.
- Calculate the count of k-length strings containing character C from the given string S using above formula.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int const M
= 1e6 + 10;
long long const mod
= 1e9 + 7;
vector< long long > fact(M);
void preCalculateFact()
{
fact[0] = 1;
fact[1] = 1;
for ( int i = 2; i < M; i++) {
fact[i] = (fact[i - 1] * i) % mod;
}
}
long long binaryExpo( long long a, long long b,
long long mod)
{
long long ans = 1;
while (b) {
if (b & 1) {
ans = (ans * a) % mod;
}
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
long long modMultiInv( long long a, long long mod)
{
return binaryExpo(a, mod - 2, mod);
}
int nCr( int n, int k)
{
return (fact[n] * modMultiInv(fact[k], mod) % mod
* modMultiInv(fact[n - k], mod) % mod)
% mod;
}
int solve( int n, int k, char c, string& s)
{
preCalculateFact();
return (nCr(n, k) * 1LL * fact[k])
- (nCr(n - 1, k) * 1LL * fact[k]);
}
int main()
{
int n = 3;
int k = 2;
char c = 'a' ;
string s = "abc";
cout << solve(n, k, c, s);
return 0;
}
|
Java
import java.util.*;
public class Main {
static final int M = ( int )1e6 + 10 ;
static final long mod = ( long )1e9 + 7 ;
static long [] fact = new long [M];
static void preCalculateFact() {
fact[ 0 ] = 1 ;
fact[ 1 ] = 1 ;
for ( int i = 2 ; i < M; i++) {
fact[i] = (fact[i - 1 ] * i) % mod;
}
}
static long binaryExpo( long a, long b, long mod) {
long ans = 1 ;
while (b > 0 ) {
if ((b & 1 ) == 1 ) {
ans = (ans * a) % mod;
}
a = (a * a) % mod;
b >>= 1 ;
}
return ans;
}
static long modMultiInv( long a, long mod) {
return binaryExpo(a, mod - 2 , mod);
}
static int nCr( int n, int k) {
return ( int )(((fact[n] * modMultiInv(fact[k], mod) % mod) * modMultiInv(fact[n - k], mod)) % mod);
}
static int solve( int n, int k, char c, String s) {
preCalculateFact();
return ( int )((nCr(n, k) * 1L * fact[k]) - (nCr(n - 1 , k) * 1L * fact[k]));
}
public static void main(String[] args) {
int n = 3 ;
int k = 2 ;
char c = 'a' ;
String s = "abc" ;
System.out.println(solve(n, k, c, s));
}
}
|
C#
using System;
class Program
{
const int M = 1000000 + 10;
const long Mod = 1000000007;
static long [] fact = new long [M];
static void PreCalculateFact()
{
fact[0] = 1;
fact[1] = 1;
for ( int i = 2; i < M; i++)
{
fact[i] = (fact[i - 1] * i) % Mod;
}
}
static long BinaryExpo( long a, long b, long mod)
{
long ans = 1;
while (b > 0)
{
if (b % 2 == 1)
{
ans = (ans * a) % mod;
}
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
static long ModMultiInv( long a, long mod)
{
return BinaryExpo(a, mod - 2, mod);
}
static int nCr( int n, int k)
{
return ( int )((fact[n] * ModMultiInv(fact[k], Mod) % Mod * ModMultiInv(fact[n - k], Mod) % Mod) % Mod);
}
static int Solve( int n, int k, char c, string s)
{
PreCalculateFact();
return ( int )((nCr(n, k) * 1L * fact[k]) - (nCr(n - 1, k) * 1L * fact[k]));
}
static void Main( string [] args)
{
int n = 3;
int k = 2;
char c = 'a' ;
string s = "abc" ;
Console.WriteLine(Solve(n, k, c, s));
}
}
|
Javascript
const mod = BigInt(1e9 + 7);
const M = 1e6 + 10;
const fact = new Array(M);
function preCalculateFact() {
fact[0] = BigInt(1);
fact[1] = BigInt(1);
for (let i = 2; i < M; i++) {
fact[i] = (fact[i - 1] * BigInt(i)) % mod;
}
}
function binaryExpo(a, b) {
let ans = BigInt(1);
while (b > BigInt(0)) {
if (b & BigInt(1)) {
ans = (ans * a) % mod;
}
a = (a * a) % mod;
b >>= BigInt(1);
}
return ans;
}
function modMultiInv(a) {
return binaryExpo(a, mod - BigInt(2));
}
function nCr(n, k) {
return (
(fact[n] * modMultiInv(fact[k]) % mod) *
modMultiInv(fact[n - k]) % mod
) % mod;
}
function solve(n, k, c, s) {
preCalculateFact();
return (
(nCr(n, k) * fact[k]) % mod -
(nCr(n - 1, k) * fact[k]) % mod
) % mod;
}
const n = 3;
const k = 2;
const c = 'a' ;
const s = "abc" ;
console.log(solve(n, k, c, s).toString());
|
Python3
mod = int ( 1e9 + 7 )
M = int ( 1e6 + 10 )
fact = [ 0 ] * M
def pre_calculate_fact():
fact[ 0 ] = 1
fact[ 1 ] = 1
for i in range ( 2 , M):
fact[i] = (fact[i - 1 ] * i) % mod
def binary_expo(a, b, mod):
ans = 1
while b:
if b & 1 :
ans = (ans * a) % mod
a = (a * a) % mod
b >> = 1
return ans
def mod_multi_inv(a, mod):
return binary_expo(a, mod - 2 , mod)
def nCr(n, k):
return (fact[n] * mod_multi_inv(fact[k], mod) % mod
* mod_multi_inv(fact[n - k], mod) % mod) % mod
def solve(n, k, c, s):
pre_calculate_fact()
return (nCr(n, k) * fact[k]
- nCr(n - 1 , k) * fact[k])
def main():
n = 3
k = 2
c = 'a'
s = "abc"
print (solve(n, k, c, s))
if __name__ = = "__main__" :
main()
|
Time Complexity: O(M), where M is size for storing factorial
Auxiliary Space: O(M)
Related Article: Counting k-Length Strings with Character C Allowing Repeated Characters (SET-2)
Share your thoughts in the comments
Please Login to comment...