📜  给定字符串数组中的超字符串

📅  最后修改于: 2021-10-28 01:38:03             🧑  作者: Mango

给定 2 个字符串XY数组,任务是找到X字符串的数量。

例子:

方法:思路是利用Hashing的概念来存储字符的频率来解决问题。请按照以下步骤解决问题:

  • 初始化大小为 26 的数组,以存储数组Y中每个字符串中每个字符[az] 的最大出现次数。
  • 现在考虑 X 中的每个字符串s
    • 检查s中每个字符的频率是否大于等于上一步得到的频率

下面是上述方法的实现:

C++
// C++ implementation for the above approach
 
#include 
using namespace std;
 
// Function to find total number of superstrings
int superstring(string X[], string Y[], int N, int M)
{
 
    // Array to store max frequency
    // Of each letter
    int maxFreq[26];
    for (int i = 0; i < 26; i++)
        maxFreq[i] = 0;
 
    for (int j = 0; j < M; j++) {
        int temp[26];
        for (int i = 0; i < 26; i++)
            temp[i] = 0;
        for (int k = 0; k < Y[j].size(); k++) {
            temp[Y[j][k] - 'a']++;
        }
        for (int i = 0; i < 26; i++) {
            maxFreq[i] = max(maxFreq[i], temp[i]);
        }
    }
 
    int ans = 0;
    for (int j = 0; j < N; j++) {
 
        // Array to find frequency of each letter in string
        // x
        int temp[26];
        for (int i = 0; i < 26; i++)
            temp[i] = 0;
        for (int k = 0; k < X[j].size(); k++) {
            temp[X[j][k] - 'a']++;
        }
        int i = 0;
        for (i = 0; i < 26; i++) {
 
            // If any frequency is less in string x than
            // maxFreq, then it can't be a superstring
            if (temp[i] < maxFreq[i]) {
                break;
            }
        }
        if (i == 26) {
 
            // Increment counter of x is a superstring
            ans++;
        }
    }
    return ans;
}
 
// Driver code
int main()
{
    // Size of array X
    int N = 4;
    // Size of array Y
    int M = 3;
 
    string X[N] = { "ceo", "alco", "caaeio", "ceai" };
    string Y[M] = { "ec", "oc", "ceo" };
 
    cout << superstring(X, Y, N, M); // Function call
    return 0;
}


Java
// Java implementation for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to find total number of superstrings
    public static int superString(String X[], String Y[],
                                  int N, int M)
    {
 
        // Array to store max frequency
        // Of each letter
        int[] maxFreq = new int[26];
        for (int i = 0; i < 26; i++)
            maxFreq[i] = 0;
 
        for (int j = 0; j < M; j++) {
            int[] temp = new int[26];
            for (int k = 0; k < Y[j].length(); k++) {
                temp[Y[j].charAt(k) - 'a']++;
            }
            for (int i = 0; i < 26; i++) {
                maxFreq[i] = Math.max(maxFreq[i], temp[i]);
            }
        }
 
        int ans = 0;
        for (int j = 0; j < N; j++) {
 
            // Array to find frequency of each letter in
            // string x
            int[] temp = new int[26];
            for (int i = 0; i < 26; i++)
                temp[i] = 0;
            for (int k = 0; k < X[j].length(); k++) {
                temp[X[j].charAt(k) - 'a']++;
            }
 
            int i = 0;
            for (i = 0; i < 26; i++) {
 
                // If any frequency is less in string x than
                // maxFreq, then it can't be a superstring
                if (temp[i] < maxFreq[i]) {
                    break;
                }
            }
            if (i == 26) {
 
                // Increment counter of x is a superstring
                ans++;
            }
        }
        return ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String[] X = new String[] { "ceo", "alco", "caaeio",
                                    "ceai" };
        String[] Y = new String[] { "ec", "oc", "ceo" };
 
        System.out.println(
            superString(X, Y, X.length, Y.length));
    }
}


Python3
# Python3 implementation for the above approach
 
# Function to find total number of superstrings
def superstring(X, Y, N, M):
 
    # Array to store max frequency
    # Of each letter
    maxFreq = [0] * 26
 
    for j in range(M):
        temp = [0] * 26
        for k in range(len(Y[j])):
            temp[ord(Y[j][k]) - ord('a')] += 1
        for i in range(26):
            maxFreq[i] = max(maxFreq[i], temp[i])
 
    ans = 0
    for j in range(N):
         
        # Array to find frequency of each letter
        # in string x
        temp = [0] * 26
        for k in range(len(X[j])):
            temp[ord(X[j][k]) - ord('a')] += 1
             
        i = 0
         
        while i < 26:
             
            # If any frequency is less in x than
            # maxFreq, then it can't be a superstring
            if (temp[i] < maxFreq[i]):
                break
            i += 1
        if (i == 26):
             
            # Increment counter of x is a superstring
            ans += 1
 
    return ans
 
# Driver code
if __name__ == '__main__':
     
    # Size of array X
    N = 4
     
    # Size of array Y
    M = 3
 
    X = ["ceo", "alco", "caaeio", "ceai"]
    Y = [ "ec", "oc", "ceo" ]
 
    print(superstring(X, Y, N, M)) #Function call
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG
{
 
    // Function to find total number of superstrings
    public static int superString(string[] X, string[] Y,
                                  int N, int M)
    {
 
        // Array to store max frequency
        // Of each letter
        int[] maxFreq = new int[26];
        for (int i = 0; i < 26; i++)
            maxFreq[i] = 0;
 
        for (int j = 0; j < M; j++) {
            int[] temp = new int[26];
            for (int k = 0; k < Y[j].Length; k++) {
                temp[Y[j][k] - 'a']++;
            }
            for (int i = 0; i < 26; i++) {
                maxFreq[i] = Math.Max(maxFreq[i], temp[i]);
            }
        }
 
        int ans = 0;
        for (int j = 0; j < N; j++) {
 
            // Array to find frequency of each letter in
            // string x
            int[] temp = new int[26];
            int i =0;
            for ( i = 0; i < 26; i++)
                temp[i] = 0;
            for (int k = 0; k < X[j].Length; k++) {
                temp[X[j][k] - 'a']++;
            }
 
            for ( i = 0; i < 26; i++) {
 
                // If any frequency is less in string x than
                // maxFreq, then it can't be a superstring
                if (temp[i] < maxFreq[i]) {
                    break;
                }
            }
            if ( i == 26) {
 
                // Increment counter of x is a superstring
                ans++;
            }
        }
        return ans;
    }
 
// Driver code
static void Main()
{
    string[] X = new String[] { "ceo", "alco", "caaeio",
                                    "ceai" };
        string[] Y = new String[] { "ec", "oc", "ceo" };
 
        Console.Write(
            superString(X, Y, X.Length, Y.Length));
}
}
 
// This code is contributed b sanjoy_62.


Javascript


输出:
2

时间复杂度: O(N*N1 + M*M1),其中 N = 数组 X 的大小,N1 = Maxlength(x),M = 数组 Y 的大小,M1 = Maxlength(y),

空间复杂度: O(1)