Count ways to place all the characters of two given strings alternately | Java program to implement the above approach ; Function to get the factorial of N ; Function to get the total number of distinct ways ; Length of str1 ; Length of str2 ; If both strings have equal length ; If both strings do not have equal length ; Driver code
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 ) ) ; } }