We are given a String and our task is to test if the string contains elements from the list.
Example:
Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True
Naive Approach checking each word in the string
Here we are splitting the string into list of words and then matching each word of this list with the already present list of words we want to check.
Python3
# initializing string test_string = "There are 2 apples for 4 persons" # initializing test list test_list = [ 'apples' , 'oranges' ] # printing original string print ( "The original string : " + test_string) # printing original list print ( "The original list : " + str (test_list)) test_string = test_string.split( " " ) flag = 0 for i in test_string: for j in test_list: if i = = j: flag = 1 break if flag = = 1 : print ( "String contains the list element" ) else : print ( "String does not contains the list element" ) |
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element
Using list comprehension to check if string contains element from list
This problem can be solved using the list comprehension, in this, we check for the list and also with string elements if we can find a match, and return true, if we find one and false is not using the conditional statements.
Python3
# Python3 code to demonstrate # checking if string contains list element # using list comprehension # initializing string test_string = "There are 2 apples for 4 persons" # initializing test list test_list = [ 'apples' , 'oranges' ] # printing original string print ( "The original string : " + test_string) # printing original list print ( "The original list : " + str (test_list)) # using list comprehension # checking if string contains list element res = [ele for ele in test_list if (ele in test_string)] # print result print ( "Does string contain any list element : " + str ( bool (res))) |
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
Using any() to check if string contains element from list
Using any function is the most classical way in which you can perform this task and also efficiently. This function checks for match in string with match of each element of list.
Python3
# checking if string contains list element # using list comprehension # initializing string test_string = "There are 2 apples for 4 persons" # initializing test list test_list = [ 'apples' , 'oranges' ] # printing original string print ( "The original string : " + test_string) # printing original list print ( "The original list : " + str (test_list)) # using list comprehension # checking if string contains list element res = any (ele in test_string for ele in test_list) # print result print ( "Does string contain any list element : " + str (res)) |
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
Using find() method to check if string contains element from list
Here we are using the find() method to check the occurrence of the word and it returns -1 if the word does not exist in the list.
Python3
# Python3 code to demonstrate # checking if string contains list element # initializing string test_string = "There are 2 apples for 4 persons" # initializing test list test_list = [ 'apples' , 'oranges' ] # printing original string print ( "The original string : " + test_string) # printing original list print ( "The original list : " + str (test_list)) # checking if string contains list element res = False c = 0 for i in test_list: if (test_string.find(i)! = - 1 ): c + = 1 if (c> = 1 ): res = True # print result print ( "Does string contain any list element : " + str ( bool (res))) |
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
Using Counter() function
Python3
# Python3 code to demonstrate # checking if string contains list element from collections import Counter # initializing string test_string = "There are 2 apples for 4 persons" # initializing test list test_list = [ 'apples' , 'oranges' ] # printing original string print ( "The original string : " + test_string) # printing original list print ( "The original list : " + str (test_list)) res = False freq = Counter(test_list) for i in test_string.split(): if i in freq.keys(): res = True # print result print ( "Does string contain any list element : " + str ( bool (res))) |
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
Time Complexity: O(N)
Auxiliary Space: O(N)
Using Set Intersection to Check if String Contains Element from List
One approach to check if a string contains an element from a list is to convert the string and the list into sets and then check for the intersection between the sets. If the intersection is not an empty set, it means that the string contains an element from the list.
Python3
# Initializing string test_string = "There are 2 apples for 4 persons" # Initializing test list test_list = [ 'apples' , 'oranges' ] # Printing original string print ( "The original string:" , test_string) # Printing original list print ( "The original list:" , test_list) # Converting string and list into sets string_set = set (test_string.split()) list_set = set (test_list) # Checking for intersection between sets if list_set.intersection(string_set): print ( "String contains an element from the list" ) else : print ( "String does not contain an element from the list" ) #This code is contributed by Edula Vinay Kumar Reddy |
The original string: There are 2 apples for 4 persons The original list: ['apples', 'oranges'] String contains an element from the list
This approach has a time complexity of O(n) and an auxiliary space of O(n).
Using operator.countOf() method.
Python3
# Python3 code to demonstrate # checking if string contains list element # initializing string import operator as op test_string = "There are 2 apples for 4 persons" # initializing test list test_list = [ 'apples' , 'oranges' ] # printing original string print ( "The original string : " + test_string) # printing original list print ( "The original list : " + str (test_list)) # checking if string contains list element res = False c = 0 for i in test_list: if (op.countOf(test_string, i) = = 0 ): c + = 1 if (c > = 1 ): res = True # print result print ( "Does string contain any list element : " + str ( bool (res))) |
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Using operator.contains() method
Approach
- Initiate a for loop to traverse list of strings and set res to False
- Check whether each string of list is present in given string
- If yes set res to True and break out of for loop
- Display res
Python3
# Python3 code to demonstrate # checking if string contains list element # initializing string import operator as op test_string = "There are 2 apples for 4 persons" # initializing test list test_list = [ 'apples' , 'oranges' ] # printing original string print ( "The original string : " + test_string) # printing original list print ( "The original list : " + str (test_list)) # checking if string contains list element res = False c = 0 for i in test_list: if (op.contains(test_string, i)): res = op.contains(test_string, i) break # print result print ( "Does string contain any list element : " + str (res)) |
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
Time Complexity : O(M*N) M-length of test_list N-length of test_string
Auxiliary Space : O(1)
Approach#9:using lambda
Algorithm
1. Take the input string and a list of words from the user.
2. Define a lambda function contains_word() using lambda, map() and any() functions to check if any element from the list is present in the string.
3. Call the contains_word() function with the input string and the list of words as arguments.
4. Print the result.
Python3
string = "There are 2 apples for 4 persons" lst = [ 'apples' , 'oranges' ] # using lambda functions to check if string contains any element from list contains_word = lambda s, l: any ( map ( lambda x: x in s, l)) # printing the result if contains_word(string, lst): print ( "String contains at least one word from list." ) else : print ( "String does not contain any word from list." ) |
String contains at least one word from list.
Time complexity: O(n*m) where n is the length of the input string and m is the length of the list of words. This is because we are using the in operator to check if each element of the list is present in the string, which takes linear time.
Space complexity: O(n) where n is the length of the input string. This is because we are storing the input string in memory. The space used by the list of words and the lambda functions is negligible compared to the input string.