Remove Words that are Common in Two Strings Last Updated : 27 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given two strings we need to remove words that are common in two strings. For example, we are having two strings s = "hello world programming is fun" a = "world is amazing programming" we need to remove the common words from both the string so that output should be "hello fun amazing" we can use multiple methods for this like set operations list comprehension.Using Set OperationsSet operations find common words using intersection (&), and list comprehension removes those words from both strings. Python s = "hello world programming is fun" a = "world is amazing programming" w = set(s.split()) b = set(a.split()) c = w & b # Find common words using set intersection # Remove common words from both strings res1 = ' '.join([word for word in s.split() if word not in c]) res2 = ' '.join([word for word in a.split() if word not in c]) print(res1) print(res2) Outputhello fun amazing Explanation:Code splits both input strings s and a into sets of words (w and b) then finds the common words using the set intersection (&), storing them in c.It then reconstructs the strings res1 and res2 by joining the words from the original strings that are not in the common words set (c)Using List Comprehension with a SetList comprehension filters out common words by checking if each word in the original strings is not in the set of common words. The result is a new string created by joining the remaining words Python a = "hello world programming is fun" b = "world is amazing programming" # Convert string 'b' into a set of words for fast look-up w = set(b.split()) # Remove words from 'a' that are present in the set 'w' (i.e., words from 'b') res1 = ' '.join([word for word in a.split() if word not in w]) # Remove words from 'b' that are present in 'a' by splitting 'a' into a list res2 = ' '.join([word for word in b.split() if word not in a.split()]) print(res1) print(res2) Outputhello fun amazing Explanation:Code splits string b into a set of words (w) then uses list comprehension to remove any words from a that are present in w and stores the result in res1.Similarly it removes words from b that are found in a and stores the result in res2 then prints both results.Using collections.CounterUsing collections.Counter the code counts the word frequencies in both strings, finds common words with set intersection (&) and removes them using list comprehension to form the updated strings. Python from collections import Counter # Split both strings into lists of words a = "hello world programming is fun" b = "world is amazing programming" c = a.split() # Split string 'a' into a list of words d = b.split() # Split string 'b' into a list of words # Count the frequency of each word in both lists counter1 = Counter(c) counter2 = Counter(d) # Find common words using set intersection c_word = set(counter1) & set(counter2) # Remove common words from both strings res1 = ' '.join([word for word in c if word not in c_word]) # Remove common words from 'a' res2 = ' '.join([word for word in d if word not in c_word]) # Remove common words from 'b' print(res1) print(res2) Outputhello fun amazing Explanation:Code splits both strings a and b into word lists counts the frequency of each word using Counter and finds the common words using set intersection.It then removes the common words from both strings and joins the remaining words to form the resulting strings res1 and res2 which are printed. Comment More infoAdvertise with us Next Article Remove Duplicate/Repeated words from String V vikkycirus Follow Improve Article Tags : Strings Hash DSA frequency-counting Python list-programs Python dictionary-programs strings +3 More Practice Tags : HashStringsStrings Similar Reads Remove minimum characters so that two strings become anagram Given two strings in lowercase, the task is to make them anagrams. The only allowed operation is to remove a character from any string. Find the minimum number of characters to be deleted to make both the strings anagram. Two strings are called anagrams of each other if one of them can be converted 12 min read Check if two strings have a common substring You are given two strings str1 and str2. You have to check if the two strings share a common substring. Examples : Input : str1 = "HELLO" str2 = "WORLD" Output : YES Explanation : The substrings "O" and "L" are common to both str1 and str2 Input : str1 = "HI" str2 = "ALL" Output : NO Explanation : B 5 min read Remove the forbidden strings Given a string W, change the string in such a way that it does not contain any of the "forbidden" strings S1 to Sn as one of its substrings. The rules that govern this change are as follows: Case of the characters does not matter i.e "XyZ" is the same as "xYZ". Change all the characters that are cov 15+ min read Remove Duplicate/Repeated words from String Given a string S, the task is to remove all duplicate/repeated words from the given string. Examples: Input: S = "Geeks for Geeks A Computer Science portal for Geeks" Output: Geeks for A Computer Science portal Explanation: here 'Geeks' and 'for' are duplicate so these words are removed from the str 4 min read Remove Duplicate/Repeated words from String Given a string S, the task is to remove all duplicate/repeated words from the given string. Examples: Input: S = "Geeks for Geeks A Computer Science portal for Geeks" Output: Geeks for A Computer Science portal Explanation: here 'Geeks' and 'for' are duplicate so these words are removed from the str 4 min read Find uncommon characters of the two strings Given two strings s1 and s2, the task is to find the uncommon characters in both strings. An uncommon character means that the character is present in only one string or in another string but not in both. The strings contain only lowercase characters and can have duplicates. Note: Output the uncommo 12 min read Like