Skip to content
Related Articles
Open in App
Not now

Related Articles

Python program to find uncommon words from two Strings

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 24 Jan, 2023
Improve Article
Save Article

Given two sentences as strings A and B. The task is to return a list of all uncommon words. A word is uncommon if it appears exactly once in any one of the sentences, and does not appear in the other sentence. Note: A sentence is a string of space-separated words. Each word consists only of lowercase letters. 

Examples:

Input : A = “Geeks for Geeks”,  B = “Learning from Geeks for Geeks”
Output : [‘Learning’, ‘from’]

Input : A = “apple banana mango” , B = “banana fruits mango”
Output : [‘apple’, ‘fruits’]

Approach 1: Every uncommon word occurs exactly once in any one of the strings. So, we make a hash to count the number of occurrences of every word, then return a list of words that occurs exactly once. Below is the implementation of the above approach: 

Python3




# Python3 program to find a list of uncommon words
 
# Function to return all uncommon words
 
 
def UncommonWords(A, B):
 
    # count will contain all the word counts
    count = {}
 
    # insert words of string A to hash
    for word in A.split():
        count[word] = count.get(word, 0) + 1
 
    # insert words of string B to hash
    for word in B.split():
        count[word] = count.get(word, 0) + 1
 
    # return required list of words
    return [word for word in count if count[word] == 1]
 
 
# Driver Code
A = "Geeks for Geeks"
B = "Learning from Geeks for Geeks"
 
# Print required answer
print(UncommonWords(A, B))

Output

['Learning', 'from']

Approach 2: Using split(),list(),set(),in and not in operators

Python3




# Python3 program to find a list of uncommon words
 
# Function to return all uncommon words
def UncommonWords(A, B):
    A=A.split()
    B=B.split()
    x=[]
    for i in A:
        if i not in B:
            x.append(i)
    for i in B:
        if i not in A:
            x.append(i)
    x=list(set(x))
    return x
             
 
# Driver Code
A = "Geeks for Geeks"
B = "Learning from Geeks for Geeks"
 
# Print required answer
print(UncommonWords(A, B))

Output

['Learning', 'from']

Approach #3: Using Counter() function

Python3




# Python3 program to find a list of uncommon words
 
# Function to return all uncommon words
from collections import Counter
 
 
def UncommonWords(A, B):
    A = A.split()
    B = B.split()
    frequency_arr1 = Counter(A)
    frequency_arr2 = Counter(B)
    result = []
 
    for key in frequency_arr1:
        if key not in frequency_arr2:
            result.append(key)
    for key in frequency_arr2:
        if key not in frequency_arr1:
            result.append(key)
 
    return result
 
 
# Driver Code
A = "Geeks for Geeks"
B = "Learning from Geeks for Geeks"
 
# Print required answer
print(UncommonWords(A, B))

Output

['Learning', 'from']

Approach 4:  using operator.countOf() method

Python3




# Python3 program to find a list of uncommon words
import operator as op
# Function to return all uncommon words
 
 
def UncommonWords(A, B):
    A = A.split()
    B = B.split()
    x = []
    for i in A:
        if op.countOf(B, i) == 0:
            x.append(i)
    for i in B:
        if op.countOf(A, i) == 0:
            x.append(i)
    x = list(set(x))
    return x
 
 
# Driver Code
A = "Geeks for Geeks"
B = "Learning from Geeks for Geeks"
 
# Print required answer
print(UncommonWords(A, B))

Output

['from', 'Learning']

Time Complexity: O(N)

Auxiliary Space : O(N)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!