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

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 = 2Output:12Explanation:Possible ways to rearrange characters of a string such that all alphabets are adjacent: { {N_{1}N_{2}M_{2}M_{1}, N_{2}N_{1}M_{2}M_{1}, N_{2}N_{1}M_{1}M_{2}, N_{1}N_{2}M_{1}M_{2}, M_{2}M_{1}N_{1}N_{2}, M_{1}M_{2}N_{2}N_{1}, M_{1}M_{2}N_{1}N_{2}, M_{2}M_{1}N_{2}N_{1}, N_{1}M_{1}M_{2}N_{2}, N_{2}M_{1}M_{2}N_{1}, N_{1}M_{2}M_{1}N_{2}, N_{2}M_{2}M_{1}N_{1}} }.

Input:N = 2, M = 4Output: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:

- Calculate the factorial of
**N + 1**say,**X**and factorial of**M**say,**Y**. - 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)