Print all Strings from array A[] having all strings from array B[] as subsequence
Last Updated :
19 May, 2021
Given two arrays A[] and B[] consisting of strings, the task is to print the strings from array A[] having all strings in B[] as a subsequence.
Examples:
Input: A[] = {"geeksforgeeks", "mapple", "twitter", "table", "Linkedin"}, B[] = {"e", "l"}
Output: mapple table linkedin
Explanation: Both the strings "e" and "l" are subsets in "mapple", "table", "linkedin".
Input: A[] = {"geeksforgeeks", "topcoder", "leetcode"}, B[] = {"geek", "ee"}
Output: geeksforgeeks
Explanation: Each string in B[], {"geek", "ee"} occurs in "geeksforgeeks" only.
Naive Approach:
The simplest approach to solve the problem is to traverse array A[] and for every string, check if all the strings in array B[] are present in it as a subsequence or not.
Time complexity: O(N2 * L), where length denotes the maximum length of a string in array A[]
Auxiliary Space: O(1)
Efficient Approach:
To optimize the above approach, follow the steps below:
- Initialize a matrix A_fre[][], in which A_fre[i] stores the frequency of each character in ith string.
- Initialize B_fre[] to store frequency of all characters in the strings in array B[].
- Traverse over array A[] and for every string, check if a character has more frequency in the strings of array B[] than in ith string in A[], i.e.
if A_fre[i][j] < B_fre[j], where
A_fre[i][j]: Frequency of the character with ASCII value ('a' + j) in ith string in A[].
B_fre[j]: Frequency of the character with ASCII value ('a' + j) in the strings of B[].
then that string has at least one string in B[] which is not its subsequence.- If the above condition is not satisfied for all characters for any string in A[], print that string as an answer.
- After checking all strings in A[], if no string is found to be having all strings in B[] as its proper subset, print -1.
Below is the implementation of the above approach:
C++
// C++ Program to implement the
// above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find strings from A[]
// having all strings in B[] as subsequence
void UniversalSubset(vector<string> A,
vector<string> B)
{
// Calculate respective sizes
int n1 = A.size();
int n2 = B.size();
// Stores the answer
vector<string> res;
// Stores the frequency of each
// character in strings of A[]
int A_fre[n1][26];
for (int i = 0; i < n1; i++) {
for (int j = 0; j < 26; j++)
A_fre[i][j] = 0;
}
// Compute the frequencies
// of characters of all strings
for (int i = 0; i < n1; i++) {
for (int j = 0; j < A[i].size();
j++) {
A_fre[i][A[i][j] - 'a']++;
}
}
// Stores the frequency of each
// character in strings of B[]
// each character of a string in B[]
int B_fre[26] = { 0 };
for (int i = 0; i < n2; i++) {
int arr[26] = { 0 };
for (int j = 0; j < B[i].size();
j++) {
arr[B[i][j] - 'a']++;
B_fre[B[i][j] - 'a']
= max(B_fre[B[i][j] - 'a'],
arr[B[i][j] - 'a']);
}
}
for (int i = 0; i < n1; i++) {
int flag = 0;
for (int j = 0; j < 26; j++) {
// If the frequency of a character
// in B[] exceeds that in A[]
if (A_fre[i][j] < B_fre[j]) {
// A string exists in B[] which
// is not a proper subset of A[i]
flag = 1;
break;
}
}
// If all strings in B[] are
// proper subset of A[]
if (flag == 0)
// Push the string in
// resultant vector
res.push_back(A[i]);
}
// If any string is found
if (res.size()) {
// Print those strings
for (int i = 0; i < res.size();
i++) {
for (int j = 0; j < res[i].size();
j++)
cout << res[i][j];
}
cout << " ";
}
// Otherwise
else
cout << "-1";
}
// Driver code
int main()
{
vector<string> A = { "geeksforgeeks",
"topcoder",
"leetcode" };
vector<string> B = { "geek", "ee" };
UniversalSubset(A, B);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG {
// Function to find strings from A[]
// having all strings in B[] as subsequence
static void UniversalSubset(List<String> A,
List<String> B)
{
// Calculate respective sizes
int n1 = A.size();
int n2 = B.size();
// Stores the answer
List<String> res = new ArrayList<>();
// Stores the frequency of each
// character in strings of A[]
int[][] A_fre = new int[n1][26];
for(int i = 0; i < n1; i++)
{
for(int j = 0; j < 26; j++)
A_fre[i][j] = 0;
}
// Compute the frequencies
// of characters of all strings
for(int i = 0; i < n1; i++)
{
for(int j = 0; j < A.get(i).length(); j++)
{
A_fre[i][A.get(i).charAt(j) - 'a']++;
}
}
// Stores the frequency of each
// character in strings of B[]
// each character of a string in B[]
int[] B_fre = new int[26];
for(int i = 0; i < n2; i++)
{
int[] arr = new int[26] ;
for(int j = 0; j < B.get(i).length(); j++)
{
arr[B.get(i).charAt(j) - 'a']++;
B_fre[B.get(i).charAt(j) - 'a'] = Math.max(
B_fre[B.get(i).charAt(j) - 'a'],
arr[B.get(i).charAt(j) - 'a']);
}
}
for(int i = 0; i < n1; i++)
{
int flag = 0;
for(int j = 0; j < 26; j++)
{
// If the frequency of a character
// in B[] exceeds that in A[]
if (A_fre[i][j] < B_fre[j])
{
// A string exists in B[] which
// is not a proper subset of A[i]
flag = 1;
break;
}
}
// If all strings in B[] are
// proper subset of A[]
if (flag == 0)
// Push the string in
// resultant vector
res.add(A.get(i));
}
// If any string is found
if (res.size() != 0)
{
// Print those strings
for(int i = 0; i < res.size(); i++)
{
for(int j = 0;
j < res.get(i).length();
j++)
System.out.print(res.get(i).charAt(j));
}
System.out.print(" ");
}
// Otherwise
else
System.out.print("-1");
}
// Driver code
public static void main (String[] args)
{
List<String> A = Arrays.asList("geeksforgeeks",
"topcoder",
"leetcode");
List<String> B = Arrays.asList("geek", "ee");
UniversalSubset(A, B);
}
}
// This code is contributed by offbeat
Python3
# Python3 program to implement
# the above approach
# Function to find strings from A[]
# having all strings in B[] as subsequence
def UniversalSubset(A, B):
# Calculate respective sizes
n1 = len(A)
n2 = len(B)
# Stores the answer
res = []
# Stores the frequency of each
# character in strings of A[]
A_freq = [[0 for x in range(26)]
for y in range(n1)]
# Compute the frequencies
# of characters of all strings
for i in range(n1):
for j in range(len(A[i])):
A_freq[i][ord(A[i][j]) - ord('a')] += 1
# Stores the frequency of each
# character in strings of B[]
# each character of a string in B[]
B_freq = [0] * 26
for i in range(n2):
arr = [0] * 26
for j in range(len(B[i])):
arr[ord(B[i][j]) - ord('a')] += 1
B_freq[ord(B[i][j]) - ord('a')] = max(
B_freq[ord(B[i][j]) - ord('a')],
arr[ord(B[i][j]) - ord('a')])
for i in range(n1):
flag = 0
for j in range(26):
# If the frequency of a character
# in B[] exceeds that in A[]
if(A_freq[i][j] < B_freq[j]):
# A string exists in B[] which
# is not a proper subset of A[i]
flag = 1
break
# If all strings in B[] are
# proper subset of A[]
if(flag == 0):
# Push the string in
# resultant vector
res.append(A[i])
# If any string is found
if(len(res)):
# Print those strings
for i in range(len(res)):
for j in range(len(res[i])):
print(res[i][j], end = "")
# Otherwise
else:
print(-1, end = "")
# Driver code
if __name__ == '__main__':
A = [ "geeksforgeeks", "topcoder",
"leetcode" ]
B = [ "geek", "ee" ]
UniversalSubset(A, B)
# This code is contributed by Shivam Singh
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find strings from []A
// having all strings in []B as subsequence
static void UniversalSubset(List<String> A,
List<String> B)
{
// Calculate respective sizes
int n1 = A.Count;
int n2 = B.Count;
// Stores the answer
List<String> res = new List<String>();
// Stores the frequency of each
// character in strings of []A
int[,] A_fre = new int[n1, 26];
for(int i = 0; i < n1; i++)
{
for(int j = 0; j < 26; j++)
A_fre[i, j] = 0;
}
// Compute the frequencies
// of characters of all strings
for(int i = 0; i < n1; i++)
{
for(int j = 0; j < A[i].Length; j++)
{
A_fre[i, A[i][j] - 'a']++;
}
}
// Stores the frequency of each
// character in strings of []B
// each character of a string in []B
int[] B_fre = new int[26];
for(int i = 0; i < n2; i++)
{
int[] arr = new int[26];
for(int j = 0; j < B[i].Length; j++)
{
arr[B[i][j] - 'a']++;
B_fre[B[i][j] - 'a'] = Math.Max(
B_fre[B[i][j] - 'a'],
arr[B[i][j] - 'a']);
}
}
for(int i = 0; i < n1; i++)
{
int flag = 0;
for(int j = 0; j < 26; j++)
{
// If the frequency of a character
// in []B exceeds that in []A
if (A_fre[i, j] < B_fre[j])
{
// A string exists in []B which
// is not a proper subset of A[i]
flag = 1;
break;
}
}
// If all strings in []B are
// proper subset of []A
if (flag == 0)
// Push the string in
// resultant vector
res.Add(A[i]);
}
// If any string is found
if (res.Count != 0)
{
// Print those strings
for(int i = 0; i < res.Count; i++)
{
for(int j = 0; j < res[i].Length; j++)
Console.Write(res[i][j]);
}
Console.Write(" ");
}
// Otherwise
else
Console.Write("-1");
}
// Driver code
public static void Main(String[] args)
{
List<String> A = new List<String>();
A.Add("geeksforgeeks");
A.Add("topcoder");
A.Add("leetcode");
List<String> B = new List<String>();
B.Add("geek");
B.Add("ee");
UniversalSubset(A, B);
}
}
// This code is contributed by amal kumar choubey
JavaScript
<script>
// Javascript Program to implement the
// above approach
// Function to find strings from A[]
// having all strings in B[] as subsequence
function UniversalSubset(A, B)
{
// Calculate respective sizes
var n1 = A.length;
var n2 = B.length;
// Stores the answer
var res = [];
// Stores the frequency of each
// character in strings of A[]
var A_fre = Array.from( Array(n1), ()=> Array(26));
for (var i = 0; i < n1; i++) {
for (var j = 0; j < 26; j++)
A_fre[i][j] = 0;
}
// Compute the frequencies
// of characters of all strings
for (var i = 0; i < n1; i++) {
for (var j = 0; j < A[i].length;
j++) {
A_fre[i][A[i].charCodeAt(j) - 'a'.charCodeAt(0)]++;
}
}
// Stores the frequency of each
// character in strings of B[]
// each character of a string in B[]
var B_fre = Array(26).fill(0);
for (var i = 0; i < n2; i++) {
var arr = Array(26).fill(0);
for (var j = 0; j < B[i].length;
j++) {
arr[B[i].charCodeAt(j) - 'a'.charCodeAt(0)]++;
B_fre[B[i].charCodeAt(j) - 'a'.charCodeAt(0)]
= Math.max(B_fre[B[i].charCodeAt(j) - 'a'.charCodeAt(0)],
arr[B[i].charCodeAt(j)- 'a'.charCodeAt(0)]);
}
}
for (var i = 0; i < n1; i++) {
var flag = 0;
for (var j = 0; j < 26; j++) {
// If the frequency of a character
// in B[] exceeds that in A[]
if (A_fre[i][j] < B_fre[j]) {
// A string exists in B[] which
// is not a proper subset of A[i]
flag = 1;
break;
}
}
// If all strings in B[] are
// proper subset of A[]
if (flag == 0)
// Push the string in
// resultant vector
res.push(A[i]);
}
// If any string is found
if (res.length>0) {
// Print those strings
for (var i = 0; i < res.length;
i++) {
for (var j = 0; j < res[i].length;
j++)
document.write( res[i][j]);
}
document.write( " ");
}
// Otherwise
else
document.write( "-1");
}
// Driver code
var A = ["geeksforgeeks",
"topcoder",
"leetcode" ];
var B = ["geek", "ee" ];
UniversalSubset(A, B);
</script>
Time Complexity: O(N * * L), where length denotes the maximum length of a string in array A[].
Auxiliary Space: O(N)
Similar Reads
Minimum cost to delete characters from String A to remove any subsequence as String B Given two strings A and B of size N and M respectively, where B is a sub-sequence of A and an array arr[] of size N, where arr[i] is the cost to delete ith character from string A. The task is to find the minimum cost to delete characters from A such that after deletion no subsequence of A is the sa
15+ min read
Print all subsequences of a string in Python Given a string s of size n (1 ⤠n ⤠20), the task is to print all subsequences of string. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.Examples: Input: s = "abc"Output: ["", "a", "b", "c", "
3 min read
Printing Longest Common Subsequence | Set 2 (Printing All) Given two sequences, print all longest subsequence present in both of them.Examples: Input: string X = "AGTGATG" string Y = "GTTAG" Output: GTAG GTTG Input: string X = "AATCC" string Y = "ACACG" Output: ACC AAC Input: string X = "ABCBDAB" string Y = "BDCABA" Output: BCAB BCBA BDAB We have discussed
12 min read
Check if a string can be obtained by appending subsequences of another string Given two strings str1 and str2 of lengths N and M respectively, the task is to check if str2 can be formed by appending subsequences of str1 multiple times. If possible, print the minimum number of append operations required. Otherwise, print -1. Examples: Input: str1 = "abb", str2 = "ababbbbb"Outp
8 min read
Print all strings in the given array that occur as the substring in the given string Given an array of string arr[] and a string str, the task is to print all the strings in arr[] that occur as a substring in str. Example: Input: str ="geeksforgeeks", arr[] ={ "forg", "geek", "ek", "dog", "sfor"}Output: forggeekeksforExplanation: The strings "forg", "geek", "ek" and "sfor" occur as
5 min read
All possible strings of any length that can be formed from a given string Given a string of distinct characters, print all possible strings of any length that can be formed from given string characters. Examples: Input: abcOutput: a b c abc ab ac bc bac bca cb ca ba cab cba acbInput: abcdOutput: a b ab ba c ac ca bc cb abc acb bac bca cab cba d ad da bd db abd adb bad bda
10 min read