Count strings from given array having all characters appearing in a given string
Last Updated :
09 Nov, 2021
Given an array of strings arr[][] of size N and a string S, the task is to find the number of strings from the array having all its characters appearing in the string S.
Examples:
Input: arr[][] = {“ab”, “aab”, “abaaaa”, “bbd”}, S = “ab”
Output: 3
Explanation: String “ab” have all the characters occurring in string S.
String “aab” have all the characters occurring in string S.
String “abaaaa” have all the characters occurring in string S.
Input:arr[] = {“geeks”, “for”, “geeks”}, S = “ds”
Output: 0
Approach: The idea is to use Hashing to solve the problem. Follow the steps below to solve the problem:
- Initialize an unordered set of characters, say valid, and a counter variable, say cnt
- Insert all the characters of the string S into the set valid.
- Traverse the array arr[] and perform the following steps:
- Finally, print the result obtained cnt
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countStrings(string S, vector<string>& list)
{
unordered_set< char > valid;
for ( auto x : S) {
valid.insert(x);
}
int cnt = 0;
for ( int i = 0; i < list.size(); i++) {
int j = 0;
for (j = 0; j < list[i].size(); j++) {
if (valid.count(list[i][j]))
continue ;
else
break ;
}
if (j == list[i].size())
cnt++;
}
return cnt;
}
int main()
{
vector<string> arr = { "ab" , "aab" ,
"abaaaa" , "bbd" };
string S = "ab" ;
cout << countStrings(S, arr) << endl;
}
|
Java
import java.util.*;
class GFG
{
static int countStrings(String S, String []list)
{
HashSet<Character> valid = new HashSet<Character>();
for ( char x : S.toCharArray())
{
valid.add(x);
}
int cnt = 0 ;
for ( int i = 0 ; i < list.length; i++)
{
int j = 0 ;
for (j = 0 ; j < list[i].length(); j++)
{
if (valid.contains(list[i].charAt(j)))
continue ;
else
break ;
}
if (j == list[i].length())
cnt++;
}
return cnt;
}
public static void main(String[] args)
{
String []arr = { "ab" , "aab" ,
"abaaaa" , "bbd" };
String S = "ab" ;
System.out.print(countStrings(S, arr) + "\n" );
}
}
|
Python3
def countStrings(S, list ):
valid = {}
for x in S:
valid[x] = 1
cnt = 0
for i in range ( len ( list )):
j = 0
while j < len ( list [i]):
if ( list [i][j] in valid):
j + = 1
continue
else :
break
j + = 1
if (j = = len ( list [i])):
cnt + = 1
return cnt
if __name__ = = '__main__' :
arr = [ "ab" , "aab" , "abaaaa" , "bbd" ]
S,l = "ab" ,[]
print (countStrings(S, arr))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countStrings(String S, String []list)
{
HashSet< char > valid = new HashSet< char >();
foreach ( char x in S.ToCharArray())
{
valid.Add(x);
}
int cnt = 0;
for ( int i = 0; i < list.Length; i++)
{
int j = 0;
for (j = 0; j < list[i].Length; j++)
{
if (valid.Contains(list[i][j]))
continue ;
else
break ;
}
if (j == list[i].Length)
cnt++;
}
return cnt;
}
public static void Main(String[] args)
{
String []arr = { "ab" , "aab" ,
"abaaaa" , "bbd" };
String S = "ab" ;
Console.Write(countStrings(S, arr) + "\n" );
}
}
|
Javascript
<script>
function countStrings(S, list)
{
let valid = new Set();
for (let x of S)
{
valid.add(x);
}
let cnt = 0;
for (let i = 0; i < list.length; i++)
{
let j = 0;
for (j = 0; j < list[i].length; j++)
{
if (valid.has(list[i][j]))
continue ;
else
break ;
}
if (j == list[i].length)
cnt++;
}
return cnt;
}
let arr = [ "ab" , "aab" ,
"abaaaa" , "bbd" ];
let S = "ab" ;
document.write(countStrings(S, arr) + "<br>" );
</script>
|
Time Complexity: O(N * M)
Auxiliary Space: O(N * M)
Share your thoughts in the comments
Please Login to comment...