![]() |
VOOZH | about |
Given a sentence as a string str and a word word, the task is to check if the word is present in str or not. A sentence is a string comprised of multiple words and each word is separated with spaces.
Examples:
Input: str = "Geeks for Geeks", word = "Geeks"
Output: Word is present in the sentence
Input: str = "Geeks for Geeks", word = "eeks"
Output: Word is not present in the sentence
Approach: In this algorithm, stringstream is used to break the sentence into words then compare each individual word of the sentence with the given word. If the word is found then the function returns true.
Note that this implementation does not search for a sub-sequence or sub-string, it only searches for a complete single word in a sentence.
Below is the implementation for the case-sensitive search approach:
Yes
Time complexity: O(n) where n is the length of the sentence.
Auxiliary space: O(n) where n is the length of string.
Below is the implementation for the case-insensitive search approach:
Yes
Auxiliary space: O(1)
Below is the implementation:
Output:
Yes
Time complexity: O(length(s))
Auxiliary space: O(1)