Count ways to place all the characters of two given strings alternately
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: 8
Explanations:
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.
Possible ways to place str1[] and str2[] alternatively
Total number of ways to rearrange all the characters of str1 = N!
Total number of ways to rearrange all the characters of str2 = M!.
Therefore, the total number of ways to place all the characters of str1 and str2 alternatively are = N! * M!
If N == M:
First place the character of str1[] and then place the character of str2[]
First place the character of str2[] and then place the character of str1[]
Total number of ways to rearrange all the characters of str1 = N!
Total number of ways to rearrange all the characters of str2 = M!
Now,
There are two cases possible here:
- First place the character of str1 and then place the character of str2.
- First place the character of str2 and then place the character of str1.
Therefore, the total number of ways = (2 * N! * M!).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int fact( int n)
{
int res = 1;
for ( int i = 1; i <= n; i++) {
res = res * i;
}
return res;
}
int distinctWays(string str1, string str2)
{
int n = str1.length();
int m = str2.length();
if (n == m) {
return 2 * fact(n) * fact(m);
}
return fact(n) * fact(m);
}
int main()
{
string str1 = "aegh" ;
string str2 = "rsw" ;
cout << distinctWays(str1, str2);
}
|
Java
import java.io.*;
class GFG{
static int fact( int n)
{
int res = 1 ;
for ( int i = 1 ; i <= n; i++)
{
res = res * i;
}
return res;
}
static int distinctWays(String str1,
String str2)
{
int n = str1.length();
int m = str2.length();
if (n == m)
{
return 2 * fact(n) * fact(m);
}
return fact(n) * fact(m);
}
public static void main (String[] args)
{
String str1 = "aegh" ;
String str2 = "rsw" ;
System.out.print(distinctWays(str1, str2));
}
}
|
Python3
def fact(n):
res = 1
for i in range ( 1 , n + 1 ):
res = res * i
return res
def distinctWays(str1, str2):
n = len (str1)
m = len (str2)
if (n = = m):
return 2 * fact(n) * fact(m)
return fact(n) * fact(m)
str1 = "aegh"
str2 = "rsw"
print (distinctWays(str1, str2))
|
C#
using System;
class GFG{
static int fact( int n)
{
int res = 1;
for ( int i = 1; i <= n; i++)
{
res = res * i;
}
return res;
}
static int distinctWays( string str1,
string str2)
{
int n = str1.Length;
int m = str2.Length;
if (n == m)
{
return 2 * fact(n) * fact(m);
}
return fact(n) * fact(m);
}
public static void Main ()
{
string str1 = "aegh" ;
string str2 = "rsw" ;
Console.Write(distinctWays(str1, str2));
}
}
|
Javascript
<script>
function fact(n) {
var res = 1;
for (i = 1; i <= n; i++) {
res = res * i;
}
return res;
}
function distinctWays( str1, str2) {
var n = str1.length;
var m = str2.length;
if (n == m) {
return 2 * fact(n) * fact(m);
}
return fact(n) * fact(m);
}
var str1 = "aegh" ;
var str2 = "rsw" ;
document.write(distinctWays(str1, str2));
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(1)
Last Updated :
07 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...