884. Uncommon Words from Two Sentences LeetCode Solution

In this guide, you will get 884. Uncommon Words from Two Sentences LeetCode Solution with the best time and space complexity. The solution to Uncommon Words from Two Sentences problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Uncommon Words from Two Sentences solution in C++
  4. Uncommon Words from Two Sentences solution in Java
  5. Uncommon Words from Two Sentences solution in Python
  6. Additional Resources
884. Uncommon Words from Two Sentences LeetCode Solution image

Problem Statement of Uncommon Words from Two Sentences

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.

Example 1:

Input: s1 = “this apple is sweet”, s2 = “this apple is sour”
Output: [“sweet”,”sour”]
Explanation:
The word “sweet” appears only in s1, while the word “sour” appears only in s2.

Example 2:

Input: s1 = “apple apple”, s2 = “banana”
Output: [“banana”]

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 the words in s1 and s2 are separated by a single space.

Complexity Analysis

  • Time Complexity:
  • Space Complexity:
See also  1116. Print Zero Even Odd LeetCode Solution

884. Uncommon Words from Two Sentences LeetCode Solution in C++

class Solution {
 public:
  vector<string> uncommonFromSentences(string A, string B) {
    vector<string> ans;
    unordered_map<string, int> count;
    istringstream iss(A + ' ' + B);

    while (iss >> A)
      ++count[A];

    for (const auto& [word, freq] : count)
      if (freq == 1)
        ans.push_back(word);

    return ans;
  }
};
/* code provided by PROGIEZ */

884. Uncommon Words from Two Sentences LeetCode Solution in Java

class Solution {
  public String[] uncommonFromSentences(String A, String B) {
    List<String> ans = new ArrayList<>();
    Map<String, Integer> count = new HashMap<>();

    for (final String word : (A + ' ' + B).split(" "))
      count.merge(word, 1, Integer::sum);

    for (final String word : count.keySet())
      if (count.get(word) == 1)
        ans.add(word);

    return ans.toArray(new String[0]);
  }
}
// code provided by PROGIEZ

884. Uncommon Words from Two Sentences LeetCode Solution in Python

class Solution:
  def uncommonFromSentences(self, A: str, B: str) -> list[str]:
    count = collections.Counter((A + ' ' + B).split())
    return [word for word, freq in count.items() if freq == 1]
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.