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++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the number of
// strings from an array having all
// characters appearing in the string S
int countStrings(string S, vector<string>& list)
{
// Initialize a set to store all
// distinct characters of string S
unordered_set<char> valid;
// Traverse over string S
for (auto x : S) {
// Insert characters
// into the Set
valid.insert(x);
}
// Stores the required count
int cnt = 0;
// Traverse the array
for (int i = 0; i < list.size(); i++) {
int j = 0;
// Traverse over string arr[i]
for (j = 0; j < list[i].size(); j++) {
// Check if character in arr[i][j]
// is present in the string S or not
if (valid.count(list[i][j]))
continue;
else
break;
}
// Increment the count if all the characters
// of arr[i] are present in the string S
if (j == list[i].size())
cnt++;
}
// Finally, print the count
return cnt;
}
// Driver code
int main()
{
vector<string> arr = { "ab", "aab",
"abaaaa", "bbd" };
string S = "ab";
cout << countStrings(S, arr) << endl;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to count the number of
// Strings from an array having all
// characters appearing in the String S
static int countStrings(String S, String []list)
{
// Initialize a set to store all
// distinct characters of String S
HashSet<Character> valid = new HashSet<Character>();
// Traverse over String S
for (char x : S.toCharArray())
{
// Insert characters
// into the Set
valid.add(x);
}
// Stores the required count
int cnt = 0;
// Traverse the array
for (int i = 0; i < list.length; i++)
{
int j = 0;
// Traverse over String arr[i]
for (j = 0; j < list[i].length(); j++)
{
// Check if character in arr[i][j]
// is present in the String S or not
if (valid.contains(list[i].charAt(j)))
continue;
else
break;
}
// Increment the count if all the characters
// of arr[i] are present in the String S
if (j == list[i].length())
cnt++;
}
// Finally, print the count
return cnt;
}
// Driver code
public static void main(String[] args)
{
String []arr = { "ab", "aab",
"abaaaa", "bbd" };
String S = "ab";
System.out.print(countStrings(S, arr) +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
# Function to count the number of
# strings from an array having all
# characters appearing in the string S
def countStrings(S, list):
# Initialize a set to store all
# distinct characters of S
valid = {}
# Traverse over S
for x in S:
# Insert characters
# into the Set
valid[x] = 1
# Stores the required count
cnt = 0
# Traverse the array
for i in range(len(list)):
j = 0
# Traverse over arr[i]
while j < len(list[i]):
# Check if character in arr[i][j]
# is present in the S or not
if (list[i][j] in valid):
j += 1
continue
else:
break
j += 1
# Increment the count if all the characters
# of arr[i] are present in the S
if (j == len(list[i])):
cnt += 1
# Finally, print the count
return cnt
# Driver code
if __name__ == '__main__':
arr = ["ab", "aab", "abaaaa", "bbd"]
S,l = "ab",[]
print(countStrings(S, arr))
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to count the number of
// Strings from an array having all
// characters appearing in the String S
static int countStrings(String S, String []list)
{
// Initialize a set to store all
// distinct characters of String S
HashSet<char> valid = new HashSet<char>();
// Traverse over String S
foreach (char x in S.ToCharArray())
{
// Insert characters
// into the Set
valid.Add(x);
}
// Stores the required count
int cnt = 0;
// Traverse the array
for (int i = 0; i < list.Length; i++)
{
int j = 0;
// Traverse over String arr[i]
for (j = 0; j < list[i].Length; j++)
{
// Check if character in arr[i,j]
// is present in the String S or not
if (valid.Contains(list[i][j]))
continue;
else
break;
}
// Increment the count if all the characters
// of arr[i] are present in the String S
if (j == list[i].Length)
cnt++;
}
// Finally, print the count
return cnt;
}
// Driver code
public static void Main(String[] args)
{
String []arr = { "ab", "aab",
"abaaaa", "bbd" };
String S = "ab";
Console.Write(countStrings(S, arr) +"\n");
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// Javascript program to implement
// the above approach
// Function to count the number of
// strings from an array having all
// characters appearing in the string S
function countStrings(S, list)
{
// Initialize a set to store all
// distinct characters of string S
let valid = new Set();
// Traverse over string S
for(let x of S)
{
// Insert characters
// into the Set
valid.add(x);
}
// Stores the required count
let cnt = 0;
// Traverse the array
for(let i = 0; i < list.length; i++)
{
let j = 0;
// Traverse over string arr[i]
for(j = 0; j < list[i].length; j++)
{
// Check if character in arr[i][j]
// is present in the string S or not
if (valid.has(list[i][j]))
continue;
else
break;
}
// Increment the count if all the characters
// of arr[i] are present in the string S
if (j == list[i].length)
cnt++;
}
// Finally, print the count
return cnt;
}
// Driver code
let arr = [ "ab", "aab",
"abaaaa", "bbd" ];
let S = "ab";
document.write(countStrings(S, arr) + "<br>");
// This code contributed by _saurabh_jaiswal
</script>
Time Complexity: O(N * M)
Auxiliary Space: O(N * M)
Similar Reads
Count all palindromic Substrings for each character in a given String Given a string S of length n, for each character S[i], the task is to find the number of palindromic substrings of length K such that no substring should contain S[i], the task is to return an array A of length n, where A[i] is the count of palindromic substrings of length K which does not include t
9 min read
Total length of string from given Array of strings composed using given characters Given a list of characters and an array of strings, find the total length of all strings in the array of strings that can be composed using the given characters.Examples: Input: string = ["mouse", "me", "bat", "lion"], chars = "eusamotb" Output: 10 Explanation: The strings that can be formed using t
6 min read
Print all valid words from given dictionary that are possible using Characters of given Array Given a dictionary of strings dict[] and a character array arr[]. The task is to print all valid words of the dictionary that are possible using characters from the given character array.Examples:Input: dict[] = ["go", "bat", "me", "eat", "goal", boy", "run"] , arr[] = ['e', 'o', 'b', 'a', 'm', 'g',
7 min read
Count of camel case characters present in a given string Given a string S, the task is to count the number of camel case characters present in the given string. The camel case character is defined as the number of uppercase characters in the given string. Examples: Input: S = "ckjkUUYII"Output: 5Explanation: Camel case characters present are U, U, Y, I an
7 min read
Count of strings in Array A that can be made equal to strings in Array B by appending characters Given two arrays of strings SearchWord[] and FindWord[]. The task is to check how many strings in FindWord[] can be formed after the following operations on as many strings as possible: Pick a string from SearchWord[].Add one English alphabet (that is not present in that string).Rearrange the string
13 min read
Check if characters of a given string can be used to form any N equal strings Given a string S and an integer N, the task is to check if it is possible to generate any string N times from the characters of the given string or not. If it is possible, print Yes. Otherwise, print No. Examples: Input: S = "caacbb", N = 2Output: YesExplanation: All possible strings that can be gen
5 min read