Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 02 Aug, 2021

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)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :