Given two strings, **str1** of length **N** and **str2** of length **M** of distinct characters, the task is to count the number of ways to place all the characters of **str1** and **str2 **alternatively.

**Note:** |N – M| ≤ 1

**Examples:**

Input:str1 =“ae”,str2 = “bd”Output:8Explanations:

Possible strings after rearrangements are : {“abed”,“ebad”,“adeb”,“edab”,“bade”,“beda”,“dabe”,“deba”}. Therefore, the required output is 8.

Input:str1= “aegh”,str2=”rsw”Output:144

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

**If N != M:** Consider only the case of **N > M** because similarly, it will work for the case** N < M**.

Total number of ways to rearrange all the characters of

str1=N!

Total number of ways to rearrange all the characters ofstr2=M!.

Therefore, the total number of ways to place all the characters ofstr1andstr2alternatively are =N! * M!

**If N == M:**

Total number of ways to rearrange all the characters of

str1=N!

Total number of ways to rearrange all the characters ofstr2=M!

Now,

There are two cases possible here:

- First place the character of
str1and then place the character ofstr2.- First place the character of
str2and then place the character ofstr1.Therefore, the total number of ways =

(2 * N! * M!).

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to get the ` `// factorial of N ` `int` `fact(` `int` `n) ` `{ ` ` ` `int` `res = 1; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `res = res * i; ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Function to get the total ` `// number of distinct ways ` `int` `distinctWays(string str1, string str2) ` `{ ` ` ` `// Length of str1 ` ` ` `int` `n = str1.length(); ` ` ` ` ` `// Length of str2 ` ` ` `int` `m = str2.length(); ` ` ` ` ` `// If both strings have equal length ` ` ` `if` `(n == m) { ` ` ` `return` `2 * fact(n) * fact(m); ` ` ` `} ` ` ` ` ` `// If both strings do not have ` ` ` `// equal length ` ` ` `return` `fact(n) * fact(m); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str1 = ` `"aegh"` `; ` ` ` `string str2 = ` `"rsw"` `; ` ` ` ` ` `cout << distinctWays(str1, str2); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `import` `java.io.*; ` ` ` `class` `GFG{ ` ` ` `// Function to get the ` `// factorial of N ` `static` `int` `fact(` `int` `n) ` `{ ` ` ` `int` `res = ` `1` `; ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `{ ` ` ` `res = res * i; ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Function to get the total ` `// number of distinct ways ` `static` `int` `distinctWays(String str1, ` ` ` `String str2) ` `{ ` ` ` ` ` `// Length of str1 ` ` ` `int` `n = str1.length(); ` ` ` ` ` `// Length of str2 ` ` ` `int` `m = str2.length(); ` ` ` ` ` `// If both strings have equal length ` ` ` `if` `(n == m) ` ` ` `{ ` ` ` `return` `2` `* fact(n) * fact(m); ` ` ` `} ` ` ` ` ` `// If both strings do not have ` ` ` `// equal length ` ` ` `return` `fact(n) * fact(m); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `String str1 = ` `"aegh"` `; ` ` ` `String str2 = ` `"rsw"` `; ` ` ` ` ` `System.out.print(distinctWays(str1, str2)); ` `} ` `} ` ` ` `// This code is contributed by code_hunt ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above approach ` ` ` `# Function to get the ` `# factorial of N ` `def` `fact(n): ` ` ` ` ` `res ` `=` `1` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` `res ` `=` `res ` `*` `i ` ` ` ` ` `return` `res ` ` ` `# Function to get the total ` `# number of distinct ways ` `def` `distinctWays(str1, str2): ` ` ` ` ` `# Length of str1 ` ` ` `n ` `=` `len` `(str1) ` ` ` ` ` `# Length of str2 ` ` ` `m ` `=` `len` `(str2) ` ` ` ` ` `# If both strings have equal length ` ` ` `if` `(n ` `=` `=` `m): ` ` ` `return` `2` `*` `fact(n) ` `*` `fact(m) ` ` ` ` ` `# If both strings do not have ` ` ` `# equal length ` ` ` `return` `fact(n) ` `*` `fact(m) ` ` ` `# Driver code ` `str1 ` `=` `"aegh"` `str2 ` `=` `"rsw"` ` ` `print` `(distinctWays(str1, str2)) ` ` ` `# This code is contributed by code_hunt ` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to get the ` `// factorial of N ` `static` `int` `fact(` `int` `n) ` `{ ` ` ` `int` `res = 1; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `{ ` ` ` `res = res * i; ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Function to get the total ` `// number of distinct ways ` `static` `int` `distinctWays(` `string` `str1, ` ` ` `string` `str2) ` `{ ` ` ` ` ` `// Length of str1 ` ` ` `int` `n = str1.Length; ` ` ` ` ` `// Length of str2 ` ` ` `int` `m = str2.Length; ` ` ` ` ` `// If both strings have equal length ` ` ` `if` `(n == m) ` ` ` `{ ` ` ` `return` `2 * fact(n) * fact(m); ` ` ` `} ` ` ` ` ` `// If both strings do not have ` ` ` `// equal length ` ` ` `return` `fact(n) * fact(m); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `string` `str1 = ` `"aegh"` `; ` ` ` `string` `str2 = ` `"rsw"` `; ` ` ` ` ` `Console.Write(distinctWays(str1, str2)); ` `} ` `} ` ` ` `// This code is contributed by code_hunt ` |

*chevron_right*

*filter_none*

**Output:**

144

**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.

## Recommended Posts:

- Create a new string by alternately combining the characters of two halves of the string in reverse
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Total number of ways to place X and Y at n places such that no two X are together
- Number of ways to place two queens on a N*N chess-board
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- Strings formed from given characters without any consecutive repeating characters
- Count ways to increase LCS length of two strings by one
- Count of same length Strings that exists lexicographically in between two given Strings
- Minimum number of operations to move all uppercase characters before all lower case characters
- Count common characters in two strings
- Count of sub-strings that do not contain all the characters from the set {'a', 'b', 'c'} at the same time
- Count of strings that become equal to one of the two strings after one removal
- Number of ways to insert two pairs of parentheses into a string of N characters
- Interleaving of two given strings with no common characters
- Minimum characters to be deleted from the end to make given two strings equal
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Print all Strings from array A[] having all strings from array B[] as subsequence
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Count ways to partition a string such that both parts have equal distinct characters

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.