![]() |
VOOZH | about |
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.
Set operations find common words using intersection (&), and list comprehension removes those words from both strings.
hello fun amazing
Explanation:
List 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
hello fun amazing
Explanation:
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.b that are found in a and stores the result in res2 then prints both results.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.
hello fun amazing
Explanation:
a and b into word lists counts the frequency of each word using Counter and finds the common words using set intersection.res1 and res2 which are printed.