Open In App

Count of Superstrings in a given array of strings

Last Updated : 09 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given 2 array of strings X and Y, the task is to find the number of superstrings in X.

A string s is said to be a Superstring, if each string present in array Y is a subsequence of string s .

Examples:

Input: X = {“ceo”, “alco”, “caaeio”, “ceai”}, Y = {“ec”, “oc”, “ceo”}
Output: 2
Explanation: Strings “ceo” and “caaeio” are superstrings as each string of array Y is a subset of these 2 strings. Other strings are not included in answer all strings of array Y are not 
sub-sets of them.

Input: X = {“iopo”, “oaai”, “iipo”}, Y = {“oo”}
Output: 1

 

Approach: The idea is to use the concept of Hashing to store the frequencies of characters to solve the problem. Follow the steps below to solve the problem:

  • Initialize an array of size 26 to store the maximum occurrences of every character[a-z] in each string present in array Y.
  • Now consider each string s in X,
    • Check if frequency of every character in s is greater than equal to the frequency obtained from the above step

Below is the implementation of the above approach:

C++




// C++ implementation for the above approach
 
#include <iostream>
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




<script>
 
// JavaScript program for the above approach
 
    // Function to find total number of superstrings
    function superString(X, Y, N, M)
    {
 
        // Array to store max frequency
        // Of each letter
        let maxFreq = Array.from({length: 26}, (_, i) => 0);
        for (let i = 0; i < 26; i++)
            maxFreq[i] = 0;
 
        for (let j = 0; j < M; j++) {
            let temp = Array.from({length: 26}, (_, i) => 0);
            for (let k = 0; k < Y[j].length; k++) {
                temp[Y[j][k].charCodeAt() - 'a'.charCodeAt()]++;
            }
            for (let i = 0; i < 26; i++) {
                maxFreq[i] = Math.max(maxFreq[i], temp[i]);
            }
        }
 
        let ans = 0;
        for (let j = 0; j < N; j++) {
 
            // Array to find frequency of each letter in
            // string x
            let temp = Array.from({length: 26}, (_, i) => 0);
            for (let i = 0; i < 26; i++)
                temp[i] = 0;
            for (let k = 0; k < X[j].length; k++) {
                temp[X[j][k].charCodeAt() - 'a'.charCodeAt()]++;
            }
 
            let 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
 
        let X = [ "ceo", "alco", "caaeio",
                                    "ceai" ];
        let Y = [ "ec", "oc", "ceo" ];
 
        document.write(
            superString(X, Y, X.length, Y.length));
 
</script>


Output: 

2

 

Time complexity: O(N*N1 + M*M1), where N = size of array X, N1 = Maxlength(x), M = size of array Y, M1 = Maxlength(y),  
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads