Uncommon Words from Two Sentences - Problem
A sentence is a string of single-space separated words where each word consists only of lowercase letters.
A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.
Given two sentences s1 and s2, return a list of all the uncommon words. You may return the answer in any order.
Input & Output
Example 1 — Basic Case
$
Input:
s1 = "this apple is sweet", s2 = "this apple is sour"
›
Output:
["sweet", "sour"]
💡 Note:
Words "this", "apple", "is" appear in both sentences. Only "sweet" and "sour" appear exactly once.
Example 2 — Single Word Sentences
$
Input:
s1 = "apple", s2 = "banana"
›
Output:
["apple", "banana"]
💡 Note:
Both words appear exactly once, so both are uncommon.
Example 3 — Repeated Words
$
Input:
s1 = "a a", s2 = "b b"
›
Output:
[]
💡 Note:
Word 'a' appears twice in s1, word 'b' appears twice in s2. No word appears exactly once.
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
- All words in s1 and s2 are separated by a single space
Visualization
Tap to expand
Understanding the Visualization
1
Input Sentences
Two sentences with space-separated words
2
Count Frequencies
Track how many times each word appears total
3
Filter Results
Return words that appear exactly once
Key Takeaway
🎯 Key Insight: A word is uncommon if it appears exactly once total across both sentences
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code