Count of strings that does not contain any character of a given string - GeeksforGeeks

23 Dec, 2021
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 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.
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++
Python3
Javascript
// C++ code for the above approach #include <bits/stdc++.h>using namespace std; // Function to count the number of strings// that does not contain any character of string strint 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 the character is present in st            if (st.find(y) != st.end()) {                allowed = 0;                break;            }        }         // If the string doesn't        // have any character of st        if (allowed) {            ans += 1;        }    }     return ans;} // Driver Codeint main(){    vector<string> arr        = { "abcd", "hijk", "xyz", "ayt" };    string str = "apple";    cout << allowedStrings(arr, str);}
# Python 3 code for the above approach # Function to count the number of strings# that does not contain any character of string strdef 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 the character is present in st1            if (y in st1):                allowed = 0                break         # If the string doesn't        # have any character of st        if (allowed):            ans += 1     return ans # Driver Codeif __name__ == "__main__":     arr = ["abcd", "hijk", "xyz", "ayt"]    st = "apple"    print(allowedStrings(arr, st))     # This code is contributed by ukasp.
<script>// Javascript code for the above approach // Function to count the number of strings// that does not contain any character of string strfunction 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 the character is present in st            if (st.has(y)) {                allowed = 0;                break;            }        }         // If the string doesn't        // have any character of st        if (allowed) {            ans += 1;        }    }     return ans;} // Driver Code let arr = ["abcd", "hijk", "xyz", "ayt"];let str = "apple";document.write(allowedStrings(arr, str)); // This code is contributed by gfgking.</script>
  
2
 
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)
 
ukasp
gfgking
frequency-counting
Arrays
Hash
Strings
Arrays
Hash
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Window Sliding Technique
Trapping Rain Water
Reversal algorithm for array rotation
Move all negative numbers to beginning and positive to end with constant extra space
Program to find sum of elements in a given array
Internal Working of HashMap in Java
Hashing | Set 1 (Introduction)
Hashing | Set 3 (Open Addressing)
Count pairs with given sum
Hashing | Set 2 (Separate Chaining)