VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-if-given-words-are-present-in-a-string/

⇱ Check if given words are present in a string - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if given words are present in a string

Last Updated : 12 Jul, 2025

Given a big string and an array of small strings, all of which are smaller in length than the big string. The task is to create an array of booleans, where each boolean represents whether the small string at that index in the array of small strings is contained in the big string. 
Note : that you can't use language-built-in string-matching methods. 
Examples:

Input : bigString = "this is a big string", smallStrings = ["this", "yo", "is", "a", "bigger", "string", "kappa"] 
Output : [true, false, true, true, false, true, false]

Input : bigString = "Mary goes to the shopping center every week.", smallStrings = ["to", "Mary", "centers", "shop", "shopping", "string", "kappa"] 
Output : [true, true, false, true, true, false, false]

Approach : Naive Approach A simple way to solve this problem is to iterate through all of the small strings, checking if each of them is contained in the big string by iterating through the big string's characters and comparing them to the given small string's characters with a couple of loops. Below is the implementation of the above approach: 


Output
true false true true false true false 

Time Complexity : O(b * n * s), where b is the length of the bigstring and n is the number of small strings and s is the length of longest small string. 
Auxiliary Space : O(n)

Approach : Using Suffix Trie Build a Suffix-trie data structure containing all of the big string's suffixes. Then, iterate through all of the small strings and check if each of them is contained in the data structure you have created. Below is the implementation of the above approach: 


Output
true false true true false true false 

Time Complexity : O(b*b + n * s), where b is the length of the bigstring and n is the number of small strings and s is the length of longest small string. 
Auxiliary Space : O(b*2 + n) 
Approach : Using Trie Try building a trie containing all of the small strings. Then, iterate through the big string's characters and check if any part of the big string is a string contained in the trie you have created. Below is the implementation of the above approach: 

Output :

true false true true false true false

Time Complexity : O(n*s + b * s), where b is the length of the bigstring and n is the number of small strings and s is the length of longest small string. 
Auxiliary Space : O(ns)

Python3 approach using find() method


Output
[True, False, True, True, False, True, False]
Comment