Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
Last Updated :
21 Nov, 2022
Given an array string[] consisting of N numeric strings of length M, the task is to find the number of distinct strings that can be generated by selecting any two strings, say i and j from the array and swap all possible prefixes between them.
Note: Since the answer can be very large, print modulo 1000000007.
Examples:
Input: N = 2 M = 3 string[] = {“112”, “211”}
Output: 4
Explanation:
Swapping “1” and “2” between the strings generates “212” and “111“.
Swapping “11” and “21” between the strings generates “212” and “111”.
Swapping “112” and “211” between the strings generates “211” and “112“.
Therefore, 4 distinct strings are generated.
Input: N = 4 M = 5 string[] = {“12121”, “23545”, “11111”, “71261”}
Output: 216
Approach: Considering a string s of the form s1s2s3s4…sm, where s1 is the first letter of any of the strings in the array, s2 is the second letter of any of the strings, and so on, the answer to the problem is the product of count(i) where count(i) is the count of different letters placed at same index in the given strings.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
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));
}
}
|
Python3
from collections import defaultdict
mod = 1000000007
def countS(string: list , n: int , m: int ) - > int :
counts = defaultdict( lambda : set ())
for i in range (n):
s = string[i]
for j in range (m):
counts[j].add(s[j])
result = 1
for index in counts:
result = (result *
len (counts[index])) % mod
return result
if __name__ = = "__main__" :
string = [ "112" , "211" ]
N = 2
M = 3
print (countS(string, N, M))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int mod = 1000000007;
public static long countS(String []str,
int n, int m)
{
Dictionary< int ,
HashSet< char > > counts = new Dictionary< int ,
HashSet< char >>();
for ( int i = 0; i < m; i++)
{
counts.Add(i, new HashSet< char >());
}
for ( int i = 0; i < n; i++)
{
String s = str[i];
for ( int j = 0; j < m; j++)
{
counts[j].Add(s[j]);
}
}
long result = 1;
foreach ( int index in counts.Keys)
result = (result * counts[index].Count) % mod;
return result;
}
public static void Main(String[] args)
{
String []str = { "112" , "211" };
int N = 2, M = 3;
Console.WriteLine(countS(str, N, M));
}
}
|
Javascript
let mod = 1000000007;
function countS(str, n, m) {
let counts = new Map();
for (let i = 0; i < m; i++) {
counts.set(i, new Set());
}
for (let i = 0; i < n; i++) {
let s = str[i];
for (let j = 0; j < m; j++) {
let temp = counts.get(j);
temp.add(s[j])
}
}
let result = 1;
for (let index of counts.keys())
result = (result * counts.get(index).size) % mod;
return result;
}
let str = [ "112" , "211" ];
let N = 2, M = 3;
console.log(countS(str, N, M));
|
Time Complexity: O(N * M)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...