Domain 1EasyCORE

Uncommon Words from Two Sentences

We are given two sentences `A` and `B`, where each sentence consists of space-separated lowercase words. The task is to identify words that appear exactly once in one of the sentences but do not appear in the other sentence. These words are considered "uncommon." We need to return a list of all such uncommon words.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#Hash Table

884. Uncommon Words from Two Sentences

Model: qwen2.5-coder:14b
Topics: Hash Table
FAANG asked: 1
Acceptance rate: 64.2%


Problem Breakdown

We are given two sentences A and B, where each sentence consists of space-separated lowercase words. The task is to identify words that appear exactly once in one of the sentences but do not appear in the other sentence. These words are considered “uncommon.” We need to return a list of all such uncommon words.

Inputs:

  • Two strings, A and B, representing the two sentences.

Outputs:

  • A list of strings containing all uncommon words from both sentences.

Implicit Constraints or Tricky Edge Cases:

  • Sentences can be empty ("").
  • Words in the sentences are lowercase letters only.
  • The length of each sentence is between 0 and 200 characters.

Intuition

A brute-force approach would involve checking each word in one sentence against all words in the other sentence, which would result in a time complexity of O(n^2), where n is the total number of words. This is inefficient given the constraints.

The key insight is to use a hash table (dictionary) to count the occurrences of each word across both sentences. By doing so, we can efficiently determine which words appear exactly once in one sentence and not in the other. This approach reduces the time complexity to O(n).

Approach

  1. Initialize a Hash Table:

    • Create an empty dictionary to store the frequency of each word.
  2. Count Words in Sentence A:

    • Split sentence A into words.
    • Iterate through each word and update its count in the hash table.
  3. Count Words in Sentence B:

    • Split sentence B into words.
    • Iterate through each word and update its count in the hash table.
  4. Identify Uncommon Words:

    • Initialize an empty list to store uncommon words.
    • Iterate through the hash table:
      • If a word has a count of exactly 1, add it to the list of uncommon words.
  5. Return the List of Uncommon Words:

Complexity Analysis

  • Time Complexity: O(n), where n is the total number of words in both sentences combined. This is because we iterate through each sentence once and perform constant-time operations for each word.
  • Space Complexity: O(n), where n is the total number of unique words across both sentences. This is due to the storage required for the hash table.

Solution

def uncommonFromSentences(A: str, B: str) -> List[str]:
    # Initialize a dictionary to count word frequencies
    word_count = {}
    
    # Count words in sentence A
    for word in A.split():
        if word in word_count:
            word_count[word] += 1
        else:
            word_count[word] = 1
    
    # Count words in sentence B
    for word in B.split():
        if word in word_count:
            word_count[word] += 1
        else:
            word_count[word] = 1
    
    # Collect uncommon words (words that appear exactly once)
    uncommon_words = [word for word, count in word_count.items() if count == 1]
    
    return uncommon_words

Edge Cases Handled

  • Empty Sentences: If either or both sentences are empty, the function will correctly return an empty list since there are no words to compare.
  • Duplicate Words within a Sentence: The hash table will correctly count duplicates and ensure that only words appearing exactly once in one sentence are considered uncommon.
  • Words Appearing in Both Sentences: Words that appear in both sentences will have a count greater than 1 and thus will not be included in the result.