Count of Superstrings in a given array of strings
Last Updated :
09 Jan, 2023
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>
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)
Similar Reads
Count of Strings of Array having given prefixes for Q query Given two arrays of strings containing words[] and queries[] having N and Q strings respectively, the task is to find the number of strings from words[] having queries[i] as the prefix for all the strings in queries[]. Examples: Input: words[] = { "geeks", "geeks", "geeks for geeks", "string", "stro
13 min read
Frequency of a string in an array of strings You are given a collection of strings and a list of queries. For every query there is a string given. We need to print the number of times the given string occurs in the collection of strings. Examples: Input : arr[] = {wer, wer, tyu, oio, tyu} q[] = {wer, tyu, uio}Output : 2 2 0Explanation : q[0] a
15 min read
Count of distinct substrings of a string using Suffix Array Given a string of length n of lowercase alphabet characters, we need to count total number of distinct substrings of this string. Examples: Input : str = âababaâ Output : 10 Total number of distinct substring are 10, which are, "", "a", "b", "ab", "ba", "aba", "bab", "abab", "baba" and "ababa"Reco
15+ min read
Count of increasing substrings in given String Given string str of length N, the task is to print the number of substrings in which every character's ASCII value is greater or equal to the ASCII value of the previous character. The substrings must at least be of length 2. Example: Input: str = "bcdabc"Output: 6Explanation: The 6 substrings with
7 min read
Count of substrings that start and end with 1 in given Binary String Given a binary string, count the number of substrings that start and end with 1. Examples: Input: "00100101"Output: 3Explanation: three substrings are "1001", "100101" and "101" Input: "1001"Output: 1Explanation: one substring "1001" Recommended PracticeCount SubstringsTry It!Count of substrings tha
12 min read
Count of strings that does not contain any character of a given string Given an array arr containing N strings and a string str, the task is to find the number of strings that do not contain any character of string str. Examples: Input: arr[] = {"abcd", "hijk", "xyz", "ayt"}, str="apple"Output: 2Explanation: "hijk" and "xyz" are the strings that do not contain any char
8 min read
Count of Distinct Substrings occurring consecutively in a given String Given a string str, the task is to find the number of distinct substrings that are placed consecutively in the given string. Examples: Input: str = "geeksgeeksforgeeks" Output: 2 Explanation: geeksgeeksforgeeks -> {"geeks"} geeksgeeksforgeeks -> {"e"} Only one consecutive occurrence of "e" is
14 min read
Number of substrings of a string Find total number of non-empty substrings of a string with N characters. Input : str = "abc" Output : 6 Every substring of the given string : "a", "b", "c", "ab", "bc", "abc" Input : str = "abcd" Output : 10 Every substring of the given string : "a", "b", "c", "d", "ab", "bc", "cd", "abc", "bcd" and
3 min read
Number of strings in two array satisfy the given conditions Given two arrays of string arr1[] and arr2[]. For each string in arr2[](say str2), the task is to count numbers string in arr1[](say str1) which satisfy the below conditions: The first characters of str1 and str2 must be equal.String str2 must contain each character of string str1.Examples: Input: a
14 min read
Count of substrings formed using a given set of characters only Given a string str and an array arr[] of K characters, the task is to find the number of substrings of str that contain characters only from the given character array arr[]. Note: The string str and the arr[] contain only lowercase alphabets. Examples: Input: S = "abcb", K = 2, charArray[] = {'a', '
8 min read