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.
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: 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:
Another way to solve this problem:
Also another way-using in-built function “symmetric_difference()”
Another way to solve this problem
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find uncommon characters of the two strings
- Find uncommon characters of the two strings | Set 2
- Concatenated string with uncommon characters of two strings
- Count words that appear exactly two times in an array of words
- Python - String uncommon characters
- Python | Uncommon elements in Lists of List
- Concatenated string with uncommon characters in Python
- Longest Uncommon Subsequence
- Shortest Uncommon Subsequence
- Check if the given string of words can be formed from words present in the dictionary
- Python | Common words among tuple strings
- Python - Remove duplicate words from Strings in List
- Python | Extract Nth words in Strings List
- Python program to find start and end indices of all Words in a String
- Sorting array of strings (or words) using Trie
- Sort an array of strings based on the frequency of good words in them
- Palindrome pair in an array of words (or strings)
- Python program to print even length words in a string
- Python program to count words in a sentence
- Python program to Concatenate Kth index words of String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.