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++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; int mod = 1000000007; // Function to count the distinct strings // possible after swapping the prefixes // between two possible strings of the array long countS(string str[],int n, int m) { // Stores the count of unique // characters for each index unordered_map<int, unordered_set<char>> counts; for(int i = 0; i < n; i++) { // Store current string string s = str[i]; for(int j = 0; j < m; j++) { counts[j].insert(s[j]); } } // Stores the total number of // distinct strings possible long result = 1; for(auto index : counts) { result = (result * counts[index.first].size()) % mod; } // Return the answer return result; } // Driver Code int main() { string str[] = { "112", "211" }; int N = 2, M = 3; cout << countS(str, N, M); return 0; } // This code is contributed by rutvik_56 |
Java
// Java Program to implement // the above approach import java.util.*; import java.io.*; public class Main { static int mod = 1000000007; // Function to count the distinct strings // possible after swapping the prefixes // between two possible strings of the array public static long countS(String str[], int n, int m) { // Stores the count of unique // characters for each index 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++) { // Store current string String s = str[i]; for (int j = 0; j < m; j++) { counts.get(j).add(s.charAt(j)); } } // Stores the total number of // distinct strings possible long result = 1; for (int index : counts.keySet()) result = (result * counts.get(index).size()) % mod; // Return the answer return result; } // Driver Code public static void main(String[] args) { String str[] = { "112", "211" }; int N = 2, M = 3; System.out.println(countS(str, N, M)); } } |
C#
// C# Program to implement // the above approach using System; using System.Collections.Generic; class GFG{ static int mod = 1000000007; // Function to count the distinct strings // possible after swapping the prefixes // between two possible strings of the array public static long countS(String []str, int n, int m) { // Stores the count of unique // characters for each index 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++) { // Store current string String s = str[i]; for (int j = 0; j < m; j++) { counts[j].Add(s[j]); } } // Stores the total number of // distinct strings possible long result = 1; foreach (int index in counts.Keys) result = (result * counts[index].Count) % mod; // Return the answer return result; } // Driver Code public static void Main(String[] args) { String []str = { "112", "211" }; int N = 2, M = 3; Console.WriteLine(countS(str, N, M)); } } // This code is contributed by Rajput-Ji |
4
Time Complexity: O(N * M)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of Palindromic Strings possible by swapping of a pair of Characters
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Count all prefixes of the given binary array which are divisible by x
- Smallest number possible by swapping adjacent even odd pairs
- Check whether two strings can be made equal by increasing prefixes
- Python | Count all prefixes in given string with greatest frequency
- Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array
- Rearrange array to make it non-decreasing by swapping pairs having GCD equal to minimum array element
- Find K for every Array element such that at least K prefixes are ≥ K
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Check if it is possible to sort an array with conditional swapping of adjacent allowed
- Check if it is possible to sort an array with conditional swapping of elements at distance K
- Prefixes with more a than b
- Shift all prefixes by given lengths
- Print N-bit binary numbers having more 1’s than 0’s in all prefixes
- Count of distinct possible strings after performing given operations
- Count of Distinct strings possible by inserting K characters in the original string
- Count distinct strings possible by replacing each character by its Morse code
- Count all distinct pairs of repeating elements from the array for every array element
- Minimize product of first N - 1 natural numbers by swapping same positioned bits of pairs
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

