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: 2
Explanation: “hijk” and “xyz” are the strings that do not contain any character of str
Input: arr[] = {“apple”, “banana”, “pear”}, str=”nil”
Output: 1
Approach: Follow the below steps to solve this problem:
- Create a set st and store each character of string str in this set.
- Create a variable ans to store the number of all the required strings.
- Now, start traversing on the array and for each string, check if any of its character is present in the set st or not. If it is, then this is not a required string.
- If it doesn’t contain any character from the set, increase answer by 1.
- Return ans after the loop ends, as the answer to this problem.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int allowedStrings(vector<string>& arr, string str)
{
int ans = 0;
unordered_set< char > st;
for ( auto x : str) {
st.insert(x);
}
for ( auto x : arr) {
bool allowed = 1;
for ( auto y : x) {
if (st.find(y) != st.end()) {
allowed = 0;
break ;
}
}
if (allowed) {
ans += 1;
}
}
return ans;
}
int main()
{
vector<string> arr
= { "abcd" , "hijk" , "xyz" , "ayt" };
string str = "apple" ;
cout << allowedStrings(arr, str);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int allowedStrings(List<String> arr,
String str)
{
int ans = 0 ;
Set<Character> st = new HashSet<Character>();
char [] chararray = str.toCharArray();
for (Character x : chararray) {
st.add(x);
}
for ( int i = 0 ; i < arr.size(); i++) {
String x = arr.get(i);
boolean allowed = true ;
char [] chararray2 = x.toCharArray();
for (Character y : chararray2) {
if (st.contains(y)) {
allowed = false ;
break ;
}
}
if (allowed) {
ans += 1 ;
}
}
return ans;
}
public static void main(String[] args)
{
List<String> arr = new ArrayList<String>();
arr.add( "abcd" );
arr.add( "hijk" );
arr.add( "xyz" );
arr.add( "ayt" );
String str = "apple" ;
System.out.println(allowedStrings(arr, str));
}
}
|
Python3
def allowedStrings(arr, st):
ans = 0
st1 = set ([])
for x in st:
st1.add(x)
for x in arr:
allowed = 1
for y in x:
if (y in st1):
allowed = 0
break
if (allowed):
ans + = 1
return ans
if __name__ = = "__main__" :
arr = [ "abcd" , "hijk" , "xyz" , "ayt" ]
st = "apple"
print (allowedStrings(arr, st))
|
C#
using System;
using System.Collections.Generic;
public class GFG{
public static int allowedStrings(List<String> arr,
String str)
{
int ans = 0;
HashSet< char > st = new HashSet< char >();
for ( int i=0;i<str.Length;i++)
{
st.Add(str[i]);
}
for ( int i = 0; i < arr.Count; i++) {
String x = arr[i];
bool allowed = true ;
for ( int j=0;j<x.Length;j++) {
if (st.Contains(x[j])) {
allowed = false ;
break ;
}
}
if (allowed== true ) {
ans += 1;
}
}
return ans;
}
static public void Main (){
List<String> arr = new List<String>();
arr.Add( "abcd" );
arr.Add( "hijk" );
arr.Add( "xyz" );
arr.Add( "ayt" );
String str = "apple" ;
Console.WriteLine(allowedStrings(arr, str));
}
}
|
Javascript
<script>
function allowedStrings(arr, str)
{
let ans = 0;
let st = new Set();
for (x of str) {
st.add(x);
}
for (x of arr) {
let allowed = 1;
for (y of x) {
if (st.has(y)) {
allowed = 0;
break ;
}
}
if (allowed) {
ans += 1;
}
}
return ans;
}
let arr = [ "abcd" , "hijk" , "xyz" , "ayt" ];
let str = "apple" ;
document.write(allowedStrings(arr, str));
</script>
|
Time Complexity: O(N*M), where N is the size of the array and M is the size of the longest string.
Auxiliary Space: O(N)
Approach:
To solve this problem is to iterate through each string in the given array and for each string, iterate through each character in the given string str. If a character in str is found in the current string, then we break the loop and move to the next string in the array. If none of the characters in str are found in the current string, then we increment a counter variable that keeps track of the number of strings that do not contain any character of str.
Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int allowedStrings(vector<string>& arr, string str)
{
int count = 0;
for (string s : arr) {
bool flag = true ;
for ( char c : str) {
if (s.find(c) != string::npos) {
flag = false ;
break ;
}
}
if (flag) {
count++;
}
}
return count;
}
int main()
{
vector<string> arr
= { "abcd" , "hijk" , "xyz" , "ayt" };
string str = "apple" ;
cout << allowedStrings(arr, str);
}
|
Java
import java.util.*;
public class Main {
public static int allowedStrings(List<String> arr, String str) {
int count = 0 ;
for (String s : arr) {
boolean flag = true ;
for ( char c : str.toCharArray()) {
if (s.indexOf(c) != - 1 ) {
flag = false ;
break ;
}
}
if (flag) {
count++;
}
}
return count;
}
public static void main(String[] args) {
List<String> arr = Arrays.asList( "abcd" , "hijk" , "xyz" , "ayt" );
String str = "apple" ;
System.out.println(allowedStrings(arr, str));
}
}
|
Python
def allowed_strings(arr, str ):
count = 0
for s in arr:
flag = True
for c in str :
if c in s:
flag = False
break
if flag:
count + = 1
return count
if __name__ = = "__main__" :
arr = [ "abcd" , "hijk" , "xyz" , "ayt" ]
str = "apple"
print (allowed_strings(arr, str ))
|
C#
using System;
using System.Collections.Generic;
public class MainClass
{
public static int allowedStrings(List< string > arr, string str)
{
int count = 0;
foreach ( string s in arr)
{
bool flag = true ;
foreach ( char c in str)
{
if (s.Contains(c))
{
flag = false ;
break ;
}
}
if (flag)
{
count++;
}
}
return count;
}
public static void Main()
{
List< string > arr = new List< string > { "abcd" , "hijk" , "xyz" , "ayt" };
string str = "apple" ;
Console.WriteLine(allowedStrings(arr, str));
}
}
|
Javascript
function allowedStrings(arr, str) {
let count = 0;
for (let s of arr) {
let flag = true ;
for (let c of str) {
if (s.indexOf(c) !== -1) {
flag = false ;
break ;
}
}
if (flag) {
count++;
}
}
return count;
}
const arr = [ "abcd" , "hijk" , "xyz" , "ayt" ];
const str = "apple" ;
console.log(allowedStrings(arr, str));
|
Time Complexity: O(N*M)
Space Complexity: O(1)
Last Updated :
22 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...