Leetcode – Uncommon Words from Two Sentences Solution in Python

Spread the love

The realm of string manipulation in programming presents a plethora of intriguing challenges. Algorithms and data structures designed to handle text processing play a crucial role in many real-world applications, from search engines to text editors. The problem of “Uncommon Words from Two Sentences” on Leetcode offers an excellent entry point to explore some of these techniques. In this detailed exposition, we’ll delve into the problem, its nuances, potential solutions, and the Python code to solve it.

Problem Statement

Given two sentences A and B, return a list of all the words that appear in one sentence and not the other, i.e., the uncommon words. You may return the list in any order.

A word is a string of letters (consisting of English alphabetic letters) separated by spaces.


  • 0 <= A.length <= 200
  • 0 <= B.length <= 200
  • A and B both contain only spaces and lowercase letters.


  1. Input: A = "this apple is sweet", B = "this apple is sour" Output: ["sweet","sour"]
  2. Input: A = "apple apple", B = "banana" Output: ["banana"]


At first glance, the problem may seem straightforward: count the words from both sentences and return the ones that only appear in one sentence. However, the catch lies in the case where words can appear multiple times in a single sentence. Such words are not uncommon, even if they don’t appear in the other sentence. This additional twist requires a bit more consideration.

Solution Strategy

The underlying approach is centered around counting words:

  1. Word Counting: Use a dictionary (or hashmap) to count the occurrences of each word in both sentences.
  2. Filtering Uncommon Words: After counting, filter out words that appear more than once in a single sentence or appear in both sentences.

Python Code:

def uncommonFromSentences(A: str, B: str) -> List[str]:
    # Combine both sentences and split into words
    words = A.split() + B.split()
    # Use a dictionary to count occurrences of each word
    word_count = {}
    for word in words:
        word_count[word] = word_count.get(word, 0) + 1

    # Filter out words that are common or repeated in one of the sentences
    return [word for word, count in word_count.items() if count == 1]

Complexity Analysis

  1. Time Complexity: Splitting the sentences takes O(N + M) time, where N is the length of sentence A, and M is the length of sentence B. Counting each word and then filtering the uncommon ones also takes linear time, so the overall time complexity is O(N + M).
  2. Space Complexity: We use additional space for the words list and the word_count dictionary. In the worst case, this can be O(N + M).

Practical Applications

While this problem is a good exercise in string manipulation and counting, similar algorithms are employed in more complex real-world scenarios:

  1. Document Similarity: Finding unique words between two documents can be a preliminary step in more sophisticated algorithms that determine the similarity or difference between texts.
  2. Search Engines: Search algorithms could use such techniques to filter out common words and focus on unique terms when indexing web pages.


The “Uncommon Words from Two Sentences” problem provides an engaging dive into string manipulation, combining basic string operations with data structure usage. While the underlying algorithm is simple, the problem serves as a testament to the importance of reading problem statements carefully and considering all edge cases. With its real-world applications in text processing and data retrieval, mastering such problems ensures a solid foundation in handling more advanced text-based challenges.

Leave a Reply