Uncommon Words from Two Sentences - Problem
Ever wondered how to find the unique elements between two collections? This problem asks you to identify uncommon words between two sentences.
Given two sentences s1 and s2, where each sentence contains lowercase words separated by single spaces, you need to find all words that appear exactly once in either sentence but not in both.
A word is considered uncommon if:
- It appears exactly once in one sentence, AND
- It doesn't appear at all in the other sentence
Example: If s1 = "this apple is sweet" and s2 = "this apple is sour", then ["sweet", "sour"] are uncommon words.
Return the result in any order.
Input & Output
example_1.py โ Basic Case
$
Input:
s1 = "this apple is sweet", s2 = "this apple is sour"
โบ
Output:
["sweet", "sour"]
๐ก Note:
Both 'sweet' and 'sour' appear exactly once in their respective sentences and don't appear in the other sentence, making them uncommon.
example_2.py โ Repeated Words
$
Input:
s1 = "apple apple", s2 = "banana"
โบ
Output:
["banana"]
๐ก Note:
'apple' appears twice in s1, so it's not uncommon. 'banana' appears exactly once and only in s2, making it uncommon.
example_3.py โ No Uncommon Words
$
Input:
s1 = "apple apple", s2 = "apple apple"
โบ
Output:
[]
๐ก Note:
'apple' appears multiple times across both sentences, so there are no uncommon words.
Constraints
- 1 โค s1.length, s2.length โค 200
- s1 and s2 consist of lowercase English letters and spaces
- s1 and s2 do not have leading or trailing spaces
- Both sentences contain at least one word
Visualization
Tap to expand
Understanding the Visualization
1
Combine Menus
List all dishes from both restaurant menus together
2
Count Dish Frequency
Count how many times each dish appears across both menus
3
Find Signature Dishes
Dishes appearing exactly once are the unique signatures of each restaurant
Key Takeaway
๐ฏ Key Insight: Just like signature restaurant dishes, words appearing exactly once across both sentences are automatically uncommon - they're unique to one context and can't be shared!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code