📌  相关文章
📜  通过交换数组中字符串对的前缀,可以计算不同字符串的数量

📅  最后修改于: 2021-09-07 02:03:48             🧑  作者: Mango

给定一个由N 个长度为M 的数字字符串组成的数组字符串 [] ,任务是通过从数组中选择任意两个字符串(比如ij)并在它们之间交换所有可能的前缀来找到可以生成的不同字符串的数量。
注意:由于答案可能非常大,请打印模 1000000007。

例子:

方法:考虑形式为 s 1 s 2 s 3 s 4 …s m的字符串s ,其中 s 1是数组中任何字符串的第一个字母, s 2是任何字符串的第二个字母,并且依此类推,问题的答案是count(i)的乘积,其中count(i)是给定字符串位于相同索引处的不同字母的计数。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include   
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> 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 > 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));
    }
}


Python3
# Python3 program to implement
# the above approach
from collections import defaultdict
 
mod = 1000000007
 
# Function to count the distinct strings
# possible after swapping the prefixes
# between two possible strings of the array
def countS(string: list, n: int, m: int) -> int:
     
    # Stores the count of unique
    # characters for each index
    counts = defaultdict(lambda: set())
 
    for i in range(n):
         
        # Store current string
        s = string[i]
 
        for j in range(m):
            counts[j].add(s[j])
 
    # Stores the total number of
    # distinct strings possible
    result = 1
 
    for index in counts:
        result = (result *
                  len(counts[index])) % mod
 
    # Return the answer
    return result
 
# Driver Code
if __name__ == "__main__":
     
    string = [ "112", "211" ]
 
    N = 2
    M = 3
     
    print(countS(string, N, M))
 
# This code is contributed by sanjeev2552


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 > counts = new Dictionary>();
 
    for (int i = 0; i < m; i++)
    {
    counts.Add(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[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



时间复杂度: O(N * M)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live