Open In App

Python program to remove words that are common in two Strings

Improve
Improve
Like Article
Like
Save
Share
Report

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.


Using Hashing:

The problem can be solved with Hashing 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
# Python program for the above approach
from collections import Counter

# Function to remove common
# words from two strings
def removeCommonWords(sent1, sent2):
  
    # Store the words present
    # in both the sentences
    sentence1 = list(sent1.split())
    sentence2 = list(sent2.split())
    
    # Calculate frequency of words
    # using Counter() function
    frequency1 = Counter(sentence1)
    frequency2 = Counter(sentence2)


    word = 0
    
    # Iterate the list consisting
    # of words in the first sentence 
    for i in range(len(sentence1)):
      
        # If word is present
        # in both the strings
        if sentence1[word] in frequency2.keys():
          
              # Remove the word
            sentence1.pop(word)
            
            # Decrease the frequency of the word
            word = word-1
        word += 1
        
    word = 0
    
    # Iterate the list consisting of
    # words in the second sentence 
    for i in range(len(sentence2)):
      
        # If word is present
        # in both the strings
        if sentence2[word] in frequency1.keys():
          
              # Remove the word
            sentence2.pop(word)
            
            # Decrease the removed word
            word = word-1
            
        word += 1
        
    # Print the remaining
    # words in the two sentences
    print(*sentence1)
    print(*sentence2)


# Driver Code

sentence1 = "sky is blue in color"
sentence2 = "raj likes sky blue color"

removeCommonWords(sentence1, sentence2)

Output
is in
raj likes

Time Complexity: O((max(N, M))2
Auxiliary Space: O(max(N, M))

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
# Python program to implement
# the above approach

# Function to return the words which
# are common in both the sentences
def commonWords(sent1, sent2):
  
    # Splitting the words in a set
    sen1 = set(sent1)
    sen2 = set(sent2)
    
    # Stores the list of common words
    common = list(sen1.intersection(sen2))
    
    # Return the list
    return common

# Function to remove all the words
# that are common in both the strings
def removeCommonWords(sent1, sent2):
  
    # Stores the words of the
    # sentences in separate lists
    sentence1 = list(sent1.split())
    sentence2 = list(sent2.split())
    
    # Find the words that are
    # common in both the sentences
    commonlist = commonWords(sentence1, 
                             sentence2)

    word = 0
    
    # Iterate the list of words
    # of the first sentence
    for i in range(len(sentence1)):
      
        # If word is common in both lists
        if sentence1[word] in commonlist:
          
              # Remove the word
            sentence1.pop(word)
            
            # Decrease the removed word
            word = word - 1
        word += 1

    word = 0
    
    # Iterate the list of words
    # of the second sentence
    for i in range(len(sentence2)):
      
        # If word is common in both lists
        if sentence2[word] in commonlist:
          
              # Remove the word
            sentence2.pop(word)
            
            # Decrease the removed word
            word = word-1
        word += 1
        
    # Print the remaining words
    # in both the sentences
    print(*sentence1)
    print(*sentence2)


# Driver Code

S1 = "sky is blue in color"
S2 = "Raj likes sky blue color"

removeCommonWords(S1, S2)

Output
is in
Raj likes

Time Complexity: O(max(N, M)) 
Auxiliary Space: O(max(N, M))

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)

Output
is in
raj likes

Time Complexity: O(n2) 
Auxiliary Space: O(n)

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)

Output
is in
raj likes

Time Complexity: O(n2)
Auxiliary Space: O(n)

Using itertools and reduce methods:

Algorithm :

  1. Split the two input sentences into lists of words.
  2. Compute the set intersection of the two lists to find common words.
  3. Filter out the common words from both sentences.
  4. 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)
#This code is contributed by Jyothi pinjala.

Output
is in
raj likes

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.

Using List Comprehension and Filter

  • Split the sentences into lists of words.
  • Find common words between the two sentences using set intersection.
  • Use filter and lambda to create modified sentences without common words.
  • Return the modified sentences.
Python3
def removeCommonWords(s1, s2):
    # Step 1: Split the sentences into words
    words_s1 = s1.split()
    words_s2 = s2.split()

    # Step 2: Find common words using set intersection
    common_words = set(words_s1) & set(words_s2)

    # Step 3: Use filter and lambda to create modified sentences without common words
    modified_s1 = ' '.join(filter(lambda word: word not in common_words, words_s1))
    modified_s2 = ' '.join(filter(lambda word: word not in common_words, words_s2))

    # Step 4: Return the modified sentences
    return modified_s1, modified_s2

# Example 1
S1 = "sky is blue in color"
S2 = "Raj likes sky blue color"
output1, output2 = removeCommonWords(S1, S2)
print(output1)
print(output2)

Output:

is in
Raj like

Space Complexity:

The space complexity is O(N + M) where N is the number of words in the first sentence (words_s1) and M is the number of words in the second sentence (words_s2). This is due to the storage of word lists and the set of common words.

Time Complexity:

The time complexity is O(N + M) as well, where N is the length of words_s1 and M is the length of words_s2. This is primarily due to the operations of splitting sentences into words, finding the set intersection, and filtering words using lambda functions.




Last Updated : 26 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads