Given two strings S1 and S2, representing sentences, the task is to print both sentences after removing all words which are present in both sentences.
Input: S1 = “sky is blue in color”, S2 =”Raj likes sky blue color “
Output: is in
Raj likes
Explanation: The common words are [ sky, blue, color ]. Removing these words from the two sentences modifies the sentences to the specified output.
Input: S1 = “learn data structures and algorithms in GeeksforGeeks“, S2 = “GeeksforGeeks is the computer science portal for Geeks“
Output: learn data structures and algorithms in
is the computer science portal for.
Approach using Hashing: The problem can be solved using Counter() function. Follow the steps below to solve the problem:
- As all the words in a sentence are separated by spaces, split the words by spaces using split() and store them in a List.
- Initialize two lists, say sentence1 and sentence2, to store the words of the two given sentences.
- Count frequencies of the words of both sentences using the Counter() function and store it in dictionaries frequency1 and frequency2.
- Traverse the list sentence1 and remove the words which are present in the dictionary frequency2.
- Traverse the list sentence2 and remove the words which are present in the dictionary frequency1.
- Print both the lists.
Below is the implementation of the above approach:
Python3
from collections import Counter
def removeCommonWords(sent1, sent2):
sentence1 = list (sent1.split())
sentence2 = list (sent2.split())
frequency1 = Counter(sentence1)
frequency2 = Counter(sentence2)
word = 0
for i in range ( len (sentence1)):
if sentence1[word] in frequency2.keys():
sentence1.pop(word)
word = word - 1
word + = 1
word = 0
for i in range ( len (sentence2)):
if sentence2[word] in frequency1.keys():
sentence2.pop(word)
word = word - 1
word + = 1
print ( * sentence1)
print ( * sentence2)
sentence1 = "sky is blue in color"
sentence2 = "raj likes sky blue color"
removeCommonWords(sentence1, sentence2)
|
Time Complexity: O((max(N, M))2)
Auxiliary Space: O(max(N, M))
Approach using Sets and Lists: Follow the steps below to solve the problem:
- As all the words in a sentence are separated by spaces, split the words by spaces using split() and store them in a List.
- Initialize two lists, say sentence1 and sentence2, to store the words of the two given sentences.
- Convert the two Lists into Sets, say sen1 and sen2.
- Now, find the set intersection of two sets, to store words that are common in both the sentences, say common.
- Traverse the List sentence1 and pop all the words which are present in the set intersection of two sentences.
- Repeat the same for the second sentence.
- Finally, print the remaining words in the two Lists.
Below is the implementation of the above approach:
Python3
def commonWords(sent1, sent2):
sen1 = set (sent1)
sen2 = set (sent2)
common = list (sen1.intersection(sen2))
return common
def removeCommonWords(sent1, sent2):
sentence1 = list (sent1.split())
sentence2 = list (sent2.split())
commonlist = commonWords(sentence1,
sentence2)
word = 0
for i in range ( len (sentence1)):
if sentence1[word] in commonlist:
sentence1.pop(word)
word = word - 1
word + = 1
word = 0
for i in range ( len (sentence2)):
if sentence2[word] in commonlist:
sentence2.pop(word)
word = word - 1
word + = 1
print ( * sentence1)
print ( * sentence2)
S1 = "sky is blue in color"
S2 = "Raj likes sky blue color"
removeCommonWords(S1, S2)
|
Time Complexity: O(max(N, M))
Auxiliary Space: O(max(N, M))
Approach Using Lists and remove():
In this approach we are using remove method in the lists to remove the common words two strings.
in this we use slip() method to covert strings into list, and we use in operator to check the common elements.
By using the remove() method we will remove the common words in the two sentences.
Python3
def removeCommonWords(sent1,sent2):
com = []
sent1 = list (sentence1.split())
sent2 = list (sentence2.split())
for i in sent1:
if i in sent2:
sent1.remove(i)
sent2.remove(i)
print ( * sent1)
print ( * sent2)
sentence1 = "sky is blue in color"
sentence2 = "raj likes sky blue color"
removeCommonWords(sentence1,sentence2)
|
Time Complexity: O(n2)
Auxiliary Space: O(n)
Approach Using Operator.countOf() function:
Python3
import operator as op
def removeCommonWords(sent1,sent2):
com = []
sent1 = list (sentence1.split())
sent2 = list (sentence2.split())
for i in sent1:
if op.countOf(sent2,i)> 0 :
sent1.remove(i)
sent2.remove(i)
print ( * sent1)
print ( * sent2)
sentence1 = "sky is blue in color"
sentence2 = "raj likes sky blue color"
removeCommonWords(sentence1,sentence2)
|
Time Complexity: O(n2)
Auxiliary Space: O(n)
Approach: itertools and reduce methods:
Algorithm :
- Split the two input sentences into lists of words.
- Compute the set intersection of the two lists to find common words.
- Filter out the common words from both sentences.
- Print the remaining words in each sentence.
Python3
import itertools
from functools import reduce
def removeCommonWords(sent1, sent2):
sent1 = list (sentence1.split())
sent2 = list (sentence2.split())
common_words = set (sent1).intersection(sent2)
sent1 = list ( filter ( lambda x: x not in common_words, sent1))
sent2 = list ( filter ( lambda x: x not in common_words, sent2))
print ( * sent1)
print ( * sent2)
sentence1 = "sky is blue in color"
sentence2 = "raj likes sky blue color"
removeCommonWords(sentence1, sentence2)
|
The time complexity: O(n^2) because of the nested loop of the set intersection operation, where n is the length of the longest sentence. The filter function has a linear time complexity of O(n) in the worst case, where n is the length of the input list.
The space complexity: O(n) because we are creating two separate lists for each sentence, and potentially creating a set that could be as large as the length of the longest sentence.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!