Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mod = 1000000007 ; long countS ( string str [ ] , int n , int m ) { unordered_map < int , unordered_set < char > > counts ; for ( int i = 0 ; i < n ; i ++ ) { string s = str [ i ] ; for ( int j = 0 ; j < m ; j ++ ) { counts [ j ] . insert ( s [ j ] ) ; } } long result = 1 ; for ( auto index : counts ) { result = ( result * counts [ index . first ] . size ( ) ) % mod ; } return result ; } int main ( ) { string str [ ] = { "112" , "211" } ; int N = 2 , M = 3 ; cout << countS ( str , N , M ) ; return 0 ; }