Open In App

Count of ways to rearrange N digits and M alphabets keeping all alphabets together

Improve
Improve
Like Article
Like
Save
Share
Report

Given two positive integers N and M representing the count of distinct digits and alphabets respectively in a string, the task to count the number of ways to rearrange the characters of the string such that all the alphabets are adjacent.

Examples:

Input: N = 2, M = 2
Output: 12
Explanation: Possible ways to rearrange characters of a string such that all alphabets are adjacent: { {N1N2M2M1, N2N1M2M1, N2N1M1M2, N1N2M1M2, M2M1N1N2, M1M2N2N1, M1M2N1N2, M2M1N2N1, N1M1M2N2, N2M1M2N1, N1M2M1N2, N2M2M1N1} }.

Input: N = 2, M = 4
Output: 144

Naive Approach: The simplest approach to solve this problem to make a string consisting of N distinct numeric characters and M distinct alphabets. Now, generate all possible permutations of the string and check if all the alphabets of the string are adjacent or not. If found to be true, then increment the count. Finally, print the count obtained.

Time Complexity: O((N + M)!)
Auxiliary Space: O(N + M)

Efficient Approach: The problem can be solved based on the following observations:

Since all alphabets are adjacent, therefore consider all the alphabets as a single character. 
Therefore, the total count of ways to rearrange the string by considering all alphabets to a single character = ((N + 1)!) * (M!) 
 

Follow the below steps to solve the problem:

  1. Calculate the factorial of N + 1 say, X and factorial of M say, Y.
  2. Finally, print the value of (X * Y).

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to find the factorial
// of the given number
int fact(int n)
{
    int ans = 1;
 
    for(int i = 2; i <= n; i++)
        ans = ans * i;
    return ans;
}
 
// Function to count ways to rearrange
// characters of the string such that
// all alphabets are adjacent.
int findComb(int N, int M)
{
 
    // Stores factorial of (N + 1)
    int x = fact(N + 1);
 
    // Stores factorial of
    int y = fact(M);
    return (x * y);
 
}
 
// Driver Code
int main()
{
   
// Given a and b
int N = 2;
int M = 2;// Function call
cout<<findComb(N, M);
}
 
// This code is contributed by ipg2016107


Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the factorial
// of the given number
static int fact(int n)
{
    int ans = 1;
 
    for(int i = 2; i <= n; i++)
        ans = ans * i;
         
    return ans;
}
 
// Function to count ways to rearrange
// characters of the String such that
// all alphabets are adjacent.
static int findComb(int N, int M)
{
     
    // Stores factorial of (N + 1)
    int x = fact(N + 1);
 
    // Stores factorial of
    int y = fact(M);
    return (x * y);
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Given a and b
    int N = 2;
    int M = 2;
     
    // Function call
    System.out.print(findComb(N, M));
}
}
 
// This code is contributed by umadevi9616


Python3




# Python program of the above approach
 
import math
 
# Function to find the factorial
# of the given number
 
 
def fact(a):
    return math.factorial(a)
 
# Function to count ways to rearrange
# characters of the string such that
# all alphabets are adjacent.
def findComb(N, M):
 
    # Stores factorial of (N + 1)
    x = fact(N + 1)
     
    # Stores factorial of
    y = fact(M)
    return (x * y)
 
 
# Driver Code
if __name__ == "__main__":
 
    # Given a and b
    N = 2
    M = 2
 
    # Function call
    print(findComb(N, M))


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
    // Function to find the factorial
// of the given number
static int fact(int n)
{
    int ans = 1;
 
    for(int i = 2; i <= n; i++)
        ans = ans * i;
    return ans;
}
 
// Function to count ways to rearrange
// characters of the string such that
// all alphabets are adjacent.
static int findComb(int N, int M)
{
 
    // Stores factorial of (N + 1)
    int x = fact(N + 1);
 
    // Stores factorial of
    int y = fact(M);
    return (x * y);
 
}
 
// Driver Code
public static void Main()
{
   
// Given a and b
int N = 2;
int M = 2;// Function call
Console.Write(findComb(N, M));
 
}
}
 
// This code is contributed by bgangwar59.


Javascript




<script>
 
        // JavaScript program for the above approach
 
        // Function to find the factorial
        // of the given number
        function fact(n)
        {
            var ans = 1;
 
            for (var i = 2; i <= n; i++)
                ans = ans * i;
            return ans;
        }
 
        // Function to count ways to rearrange
        // characters of the string such that
        // all alphabets are adjacent.
        function findComb(N, M)
        {
 
            // Stores factorial of (N + 1)
            var x = fact(N + 1)
 
            // Stores factorial of
            var y = fact(M)
            return (x * y)
 
        }
         
        // Driver Code
 
        // Given a and b
        var N = 2
        var M = 2
 
        // Function call
        document.write(findComb(N, M))
 
// This code is contributed by Potta Lokesh
    </script>


Output: 

12

 

Time Complexity: O(N + M)
Auxiliary Space: O(1)

Approach 3 : 

The approach used in this code is based on combinatorics and the factorial of N and M.

The function fact(n) calculates the factorial of a given number, which is the product of all integers from 1 to n. This function takes O(n) time as it is a simple for loop that iterates from 2 to n, multiplying the result by the current number in each iteration.

The function findComb(N, M) calculates the total number of ways to rearrange the characters such that all alphabets are adjacent, by multiplying the factorial of N+1 and M. The factorial of N+1 is used to account for the number of ways to arrange the digits around the alphabets, and the factorial of M is used to account for the number of ways to arrange the alphabets together.

C++




#include<bits/stdc++.h>
using namespace std;
 
// Function to find the factorial
// of the given number
int fact(int n)
{
    int ans = 1;
 
    for(int i = 2; i <= n; i++)
        ans = ans * i;
    return ans;
}
 
// Function to count ways to rearrange
// characters of the string such that
// all alphabets are adjacent.
int findComb(int N, int M)
{
 
    // Stores factorial of (N + 1)
    int x = fact(N + 1);
 
    // Stores factorial of
    int y = fact(M);
    return (x * y);
 
}
 
// Driver Code
int main()
{
    // Given a and b
    int N = 2;
    int M = 2;
    // Function call
    cout<<findComb(N, M);
}
// this code is contributed by devendrasalunke


Java




public class Main {
 
    // Function to find the factorial
    // of the given number
    static int fact(int n) {
        int ans = 1;
        for (int i = 2; i <= n; i++) {
            ans = ans * i;
        }
        return ans;
    }
 
    // Function to count ways to rearrange
    // characters of the string such that
    // all alphabets are adjacent.
    static int findComb(int N, int M) {
        // Stores factorial of (N + 1)
        int x = fact(N + 1);
 
        // Stores factorial of
        int y = fact(M);
        return (x * y);
    }
 
    public static void main(String[] args) {
        // Given a and b
        int N = 2;
        int M = 2;
        // Function call
        System.out.println(findComb(N, M));
    }
}


Python3




# Python code implemetation for the above approach
 
# Function to find the factorial of the given number
def fact(n):
    ans = 1
    for i in range(2, n+1):
        ans = ans * i
    return ans
 
# Function to count ways to rearrange characters of
 # the string such that all alphabets are adjacent.
def findComb(N, M):
    # Stores factorial of (N + 1)
    x = fact(N + 1)
 
    # Stores factorial of
    y = fact(M)
    return (x * y)
 
 
# Given N and M
N = 2
M = 2
 
# Function call
print(findComb(N, M))
 
# This code is contributed by karthik.


C#




// C# code addition for the above approach
using System;
 
public class GFG {
 
  // Function to find the factorial of the given number
  static int fact(int n)
  {
    int ans = 1;
    for (int i = 2; i <= n; i++) {
      ans = ans * i;
    }
    return ans;
  }
 
  // Function to count ways to rearrange characters of the
  // string such that all alphabets are adjacent.
  static int findComb(int N, int M)
  {
    // Stores factorial of (N + 1)
    int x = fact(N + 1);
 
    // Stores factorial of
    int y = fact(M);
    return (x * y);
  }
 
  static public void Main()
  {
 
    // Code
    // Given a and b
    int N = 2;
    int M = 2;
    // Function call
    Console.WriteLine(findComb(N, M));
  }
}
 
// This code is contributed by sankar.


Javascript




// Function to find the factorial
// of the given number
function fact(n) {
  let ans = 1;
  for (let i = 2; i <= n; i++) {
    ans = ans * i;
  }
  return ans;
}
 
// Function to count ways to rearrange
// characters of the string such that
// all alphabets are adjacent.
function findComb(N, M) {
  // Stores factorial of (N + 1)
  let x = fact(N + 1);
 
  // Stores factorial of
  let y = fact(M);
  return (x * y);
}
 
// Given a and b
let N = 2;
let M = 2;
// Function call
console.log(findComb(N, M));
// this code is contributed by writer


Output

12

Time complexity : O(n) as it only involves one for loop in the fact(n) function and the rest of the calculations are constant time operations. This approach is simple and efficient as it only requires a single for loop to calculate the factorial of N and M and the rest of the calculations are simple arithmetic operations.

Auxiliary Space : O(1)



Last Updated : 01 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads