Algorithm


Problem Name: 884. 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"]

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.

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    vector<string> uncommonFromSentences(string A, string B) {
        unordered_mapm;
        vector < string>res;
        stringstream ss(A + " " + B);
        string t;
        while(ss>>t) m[t]++;
        for(auto& x: m) if(x.second == 1) res.push_back(x.first);
        return res;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
s1 = "this apple is sweet", s2 = "this apple is sour"

Output

x
+
cmd
["sweet","sour"]

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  public String[] uncommonFromSentences(String s1, String s2) {
    Map map = new HashMap<>();
    String[] wordsOne = s1.split("\\s+");
    for (String word : wordsOne) {
      map.put(word, map.getOrDefault(word, 0) + 1);
    }
    String[] wordsTwo = s2.split("\\s+");
    for (String word : wordsTwo) {
      map.put(word, map.getOrDefault(word, 0) + 1);
    }
    List < String> result = new ArrayList<>();
    for (String key : map.keySet()) {
      if (map.get(key) == 1) {
        result.add(key);
      }
    }
    return result.toArray(new String[]{});
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s1 = "this apple is sweet", s2 = "this apple is sour"

Output

x
+
cmd
["sweet","sour"]

#3 Code Example with Javascript Programming

Code - Javascript Programming


const uncommonFromSentences = function(s1, s2) {
  const hash = {}
  const a1 = s1.split(' '), a2 = s2.split(' ')
  for(let e of a1) {
    if(hash[e] == null) hash[e] = 0
    hash[e]++
  }
  const res = []
  for(let e of a2) {
    if(hash[e] == null) hash[e] = 0
    hash[e]++
  }
  Object.keys(hash).forEach(k => {
    if(hash[k] === 1) res.push(k)
  })
  
  return res
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
s1 = "apple apple", s2 = "banana"

Output

x
+
cmd
["banana"]

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def uncommonFromSentences(self, A: str, B: str) -> List[str]:
        c1 = collections.Counter(A.split())
        c2 = collections.Counter(B.split())
        return list(c for c in c1 if c1[c] == 1 and c not in c2) + list(c for c in c2 if c2[c] == 1 and c not in c1)
Copy The Code & Try With Live Editor

Input

x
+
cmd
s1 = "apple apple", s2 = "banana"

Output

x
+
cmd
["banana"]

#5 Code Example with C# Programming

Code - C# Programming


using System.Collections.Generic;
using System.Linq;

namespace LeetCode
{
    public class _0884_UncommonWordsFromTwoSentences
    {
        public string[] UncommonFromSentences(string A, string B)
        {
            var counts = new Dictionary < string, int>();
            foreach (var word in A.Split())
                if (counts.ContainsKey(word))
                    counts[word]++;
                else
                    counts[word] = 1;

            foreach (var word in B.Split())
                if (counts.ContainsKey(word))
                    counts[word]++;
                else
                    counts[word] = 1;

            return counts.Where(p => p.Value == 1).Select(p => p.Key).ToArray();
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s1 = "this apple is sweet", s2 = "this apple is sour"

Output

x
+
cmd
["sweet","sour"]
Advertisements

Demonstration


Previous
#883 Leetcode Projection Area of 3D Shapes Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#885 Leetcode Spiral Matrix III Solution in C, C++, Java, JavaScript, Python, C# Leetcode