Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array | Java Program to implement the above approach ; Function to count the distinct strings possible after swapping the prefixes between two possible strings of the array ; Stores the count of unique characters for each index ; Store current string ; Stores the total number of distinct strings possible ; Return the answer ; Driver Code
import java . util . * ; import java . io . * ; public class Main { static int mod = 1000000007 ; public static long countS ( String str [ ] , int n , int m ) { Map < Integer , Set < Character > > counts = new HashMap < > ( ) ; for ( int i = 0 ; i < m ; i ++ ) { counts . put ( i , new HashSet < > ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { String s = str [ i ] ; for ( int j = 0 ; j < m ; j ++ ) { counts . get ( j ) . add ( s . charAt ( j ) ) ; } } long result = 1 ; for ( int index : counts . keySet ( ) ) result = ( result * counts . get ( index ) . size ( ) ) % mod ; return result ; } public static void main ( String [ ] args ) { String str [ ] = { "112" , "211" } ; int N = 2 , M = 3 ; System . out . println ( countS ( str , N , M ) ) ; } }