Find Unique Words Across Two Text Blocks
StringsModel answerLast updated
Identify words that appear in either of two given text strings, but not in both, using symmetric set difference.
Write a function unique_words(text1: str, text2: str) -> list[str] that processes two text strings and identifies all words that occur in exactly one of the strings. In set theory, this corresponds to finding the symmetric difference between the word sets of both inputs.
Rules & Expectations
- Duplicates: Multiple occurrences of a word within the same string should only yield a single instance of that word in the final output.
- Exclusions: Any word present in both
text1andtext2must be excluded from the final output. - Ordering: The output list can return words in any order.
- Tokenization & Case: Unless specified otherwise during clarification, assume words are separated by whitespace and comparison should be treated as case-insensitive (normalize to lowercase).
Example 1
text1 = "apple banana apple"
text2 = "banana cherry"
# Output: ["apple", "cherry"] (or ["cherry", "apple"])
# Explanation: "banana" is in both strings. "apple" is only in text1, and "cherry" is only in text2.
Example 2
text1 = "Hello World"
text2 = "hello world"
# Output: []
# Explanation: Case-insensitive normalization makes both sets identical ({"hello", "world"}), so no unique words exist.
Model answer
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Method Explanation
The core requirement is to determine words present in one text string but absent in the other. This is fundamentally a set symmetric difference problem ($A \Delta B = (A \setminus B) \cup (B \setminus A)$).
- Preprocessing & Tokenization: First, split each string into tokens by whitespace. Convert every token to lowercase so that word comparisons are case-insensitive (e.g.,
"Hello"matches"hello"). - Set Construction: Convert the token lists into Python
setobjects. Set creation naturally deduplicates repetitive words within the same string (e.g.,"apple banana apple"becomes{"apple", "banana"}). - Symmetric Difference: Compute the symmetric difference between the two sets using Python's
^operator or the.symmetric_difference()set method. - Output: Convert the resulting set back into a list.
Python Source Code
def unique_words(text1: str, text2: str) -> list[str]:
"""
Returns a list of words that appear in text1 or text2, but not both.
Comparisons are case-insensitive and ignore extra whitespace.
"""
# Step 1: Tokenize and normalize to lowercase
words1 = set(text1.lower().split())
words2 = set(text2.lower().split())
# Step 2: Compute symmetric difference
unique_set = words1 ^ words2
# Step 3: Return result as a list
return list(unique_set)
Complexity Analysis
- Time Complexity: $\mathcal{O}(N + M)$, where $N$ is the character length of
text1and $M$ is the character length oftext2. Splitting the string takes linear time relative to its length, and set operations run in expected $\mathcal{O}(U)$ time where $U$ is the total number of unique words. - Space Complexity: $\mathcal{O}(N + M)$ to store the tokens and sets in memory.
Edge Cases and Corner Scenarios
- Empty Inputs: If both inputs are empty strings
"", the sets are empty, returning[]. If one string is empty, all unique tokens from the non-empty string are returned. - Identical Inputs: If
text1andtext2contain the exact same set of words, the result is[]. - Different Casing:
"Cat"and"cat"are normalized to"cat"to avoid false duplicates. - Multiple Whitespaces: Python's
.split()without arguments automatically handles arbitrary whitespace, tabs, and newlines.