Check if the Word is present in Sentence or not

You have been given a sentence ‘S’ in the form of a string and a word ‘W’, you need to find whether the word is present in the given sentence or not. The word must be present as a complete string in the sentence and not a substring of some other word.

Note:

1. All the characters in the string and the word are in lowercase.
2. Length of the sentences and the words will always be greater than zero.
3. Words in the sentence will be separated by spaces.

Input Format :

The first line of the input contains an integer ‘T’ denoting the number of test cases.

The first line of each test case contains the sentence ‘S’.

The second line of each test case contains the word ‘W’.

Output Format :

The only line of output of each test case should print “Yes” if the word ‘W’ is present in the sentence ‘S’, else print “No”.

The output of each test case will be printed in a separate line.

Note: You are not required to print the expected output; it has already been taken care of. Just implement the function.

Constraints :

1 <= T <= 50
1 <= |S|, |W| <= 10000

Where ‘T’ is the number of test cases, ‘S’ and ‘W’ are strings as described in the problem statement.

Time limit: 1 sec
CodingNinjas
author
2y
Brute Force

The basic idea of this approach is to check each word of the given sentence ‘S’ if it matches with the given word ‘W’.


 

Consider the following steps:

  1. Start iterating through each character of sentence string ‘S’ using a variable ‘i’ such that 0 <= ‘i’ < |S|
    • Create a string “temp” which stores the current word.
    • Add all the subsequent characters of the sentence till space is detected or if all end of the string is reached.
    • Check if the word ‘W’ matches with “temp”. Return true if it matches.
  2. After the loop ends return false because the word is not present in the sentence.
Space Complexity: O(n)Explanation:

O(|S|), where |S| is the length of the string.

 

Since we are using a variable to store the words of the sentence string and in the worst case, the whole string can be a single word. So the overall space complexity will be O(|S|).

Time Complexity: O(n)Explanation:

O(|S|), where |S| is the length of the string.

 

Since we are iterating through the input sentence string ‘S’ once and checking if the word matches with the given word ‘W’. So the overall time complexity will be O(|S|). Please refer here for more details link


Java (SE 1.8)
/*

Time Complexity: O(N)
Space Complexity: O(1)

where N is the length input sentence S.
*/

public class Solution
{

public static boolean findWord(String S, String W)
{

int n = S.length();

for (int i = 0; i < n; i++) {

StringBuilder temp = new StringBuilder();

while (i < n && S.charAt(i) != ' ')
{
temp.append(S.charAt(i));
i++;
}

if (temp.toString().compareTo(W) == 0)
{
return true;
}
}
return false;
}
}

Python (3.5)
'''

Time Complexity: O(|S|)
Space Complexity: O(|S|)

where |S| is the length of the input sentence.
'''

def findWord(s, w):
n = len(s)

i = 0

while i < n:
# To store the current word.
temp = ""

# Processing the string to find individual words.
while (i < n and s[i] != ' '):
temp += s[i]
i += 1

# Checking if the current word is equal to W or not.
if (temp == w):
return True
i += 1

# If no word matches, then return false.
return False

C++ (g++ 5.4)
/*
Time Complexity: O(|S|)
Space Complexity: O(|S|)

where |S| is the length of the input sentence.
*/

bool findWord(string &s, string w)
{
int n = s.size();

for (int i = 0; i < n; i++)
{

// To store the current word.
string temp = "";

// Processing the string to find individual words.
while (i < n && s[i] != ' ')
{
temp += s[i];
i++;
}

// Checking if the current word is equal to W or not.
if (temp == w)
{
return true;
}
}

// If no word matches, then return false.
return false;
}
CodingNinjas
author
2y
String processing using libraries

In the last approach, we were explicitly storing the words of the sentence in a set, but there are some in-built libraries (like stringstream in C++) that can do this ...read more

Help your peers!
Add answer anonymously...
UnitedHealth Software Engineer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter