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
Find Uncommon Words Across Two SentencesSentence 1: "this apple is sweet"Sentence 2: "this apple is sour"Word Frequency Analysis"this"Count: 2"apple"Count: 2"is"Count: 2"sweet"Count: 1 ✓"sour"Count: 1 ✓Uncommon Words["sweet", "sour"]Words appearing exactly once are uncommon
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
Asked in
Amazon 15 Google 12 Microsoft 8
28.5K Views
Medium Frequency
~15 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen