Open In App

Python program to find uncommon words from two Strings

Last Updated : 12 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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']

Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of the list.

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']

Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.

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)

Method 5: Using the set() and difference() method to find the difference of two sets.

Step-by-step approach:

  • Create two sets, setA and setB, by splitting string A and B into words using the split() method.
  • Use the difference() method to find the uncommon words in setA and setB.
  • Combine the uncommon words from both sets using the union() method.
  • Return the combined set as a list using the list() method.
  • Call the UncommonWords function with the given values of A and B.
  • Print the output of the function.

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):
     
    # split the strings A and B into words and create sets
    setA = set(A.split())
    setB = set(B.split())
     
    # find the uncommon words in setA and setB and combine them
    uncommonWords = setA.difference(setB).union(setB.difference(setA))
     
    # convert the set to a list and return
    return list(uncommonWords)
 
# 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), where n is the total number of words in both strings A and B.
Auxiliary space: O(n), where n is the total number of words in both strings A and B, due to the creation of two sets and one list.



Similar Reads

Find uncommon characters of the two strings | Set 2
Given two strings, str1 and str2, the task is to find and print the uncommon characters of the two given strings in sorted order without using extra space. Here, an uncommon character means that either the character is present in one string or it is present in the other string but not in both. The strings contain only lowercase characters and can c
6 min read
Find uncommon characters of the two strings
Find and print the uncommon characters of the two given strings in sorted order. Here uncommon character means that either the character is present in one string or it is present in another string but not in both. The strings contain only lowercase characters and can contain duplicates. Source: Amazon Interview Experience | Set 355 (For 1 Year Expe
15+ min read
Find resultant string after concatenating uncommon characters of given strings
Given two strings S1 and S2. The task is to concatenate uncommon characters of the S2 to S1 and return the resultant string S1 . Examples: Input: S1 = "aacdb", S2 = "gafd"Output: "cbgf" Input: S1 = "abcs", S2 = "cxzca";Output: "bsxz" Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. Method 1: Using Hashmap Approac
13 min read
Python Program to print all distinct uncommon digits present in two given numbers
Given two positive integers A and B, the task is to print the distinct digits in descending order, which are not common in the two numbers. Examples: Input: A = 378212, B = 78124590Output: 9 5 4 3 0Explanation: All distinct digits present in the two numbers are {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. The digits {1, 2, 6, 7} are common in both numbers. Inpu
5 min read
Python Program to Print uncommon elements from two sorted arrays
Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50} Output : 10 25 40 50 We do not print 20 and 30 as these elements are present in both arrays. Input : arr1[] = {10, 20
3 min read
C++ Program to Print uncommon elements from two sorted arrays
Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50} Output : 10 25 40 50 We do not print 20 and 30 as these elements are present in both arrays. Input : arr1[] = {10, 20
2 min read
Java Program to Print uncommon elements from two sorted arrays
Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50} Output : 10 25 40 50 We do not print 20 and 30 as these elements are present in both arrays. Input : arr1[] = {10, 20
2 min read
Php Program to Print uncommon elements from two sorted arrays
Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50} Output : 10 25 40 50 We do not print 20 and 30 as these elements are present in both arrays. Input : arr1[] = {10, 20
2 min read
Javascript Program to Print uncommon elements from two sorted arrays
Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50} Output : 10 25 40 50 We do not print 20 and 30 as these elements are present in both arrays. Input : arr1[] = {10, 20
2 min read
Print uncommon elements from two sorted arrays
Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50} Output : 10 25 40 50 We do not print 20 and 30 as these elements are present in both arrays. Input : arr1[] = {10, 20
6 min read