Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/884.html
884. Uncommon Words from Two Sentences (Easy)
We are given two sentences A
and B
. (A sentence is a string of space separated words. 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.
Return a list of all uncommon words.
You may return the list in any order.
Example 1:
Input: A = "this apple is sweet", B = "this apple is sour" Output: ["sweet","sour"]
Example 2:
Input: A = "apple apple", B = "banana" Output: ["banana"]
Note:
0 <= A.length <= 200
0 <= B.length <= 200
A
andB
both contain only spaces and lowercase letters.
Related Topics:
Hash Table
Solution 1.
// OJ: https://leetcode.com/problems/uncommon-words-from-two-sentences/
// Time: O(A+B)
// Space: O(A+B)
class Solution {
private:
unordered_map<string, int> getCounts(string s) {
istringstream ss(s);
string word;
unordered_map<string, int> m;
while (ss >> word) m[word]++;
return m;
}
public:
vector<string> uncommonFromSentences(string A, string B) {
auto m = getCounts(A), n = getCounts(B);
vector<string> ans;
for (auto &p : m) {
if (p.second == 1 && n.find(p.first) == n.end()) ans.push_back(p.first);
}
for (auto &p : n) {
if (p.second == 1 && m.find(p.first) == m.end()) ans.push_back(p.first);
}
return ans;
}
};
Java
-
class Solution { public String[] uncommonFromSentences(String A, String B) { List<String> uncommonWords = new ArrayList<String>(); Set<String> set = new HashSet<String>(); String[] strA = A.split(" "); String[] strB = B.split(" "); for (String word : strA) { if (set.add(word)) uncommonWords.add(word); else uncommonWords.remove(word); } for (String word : strB) { if (set.add(word)) uncommonWords.add(word); else uncommonWords.remove(word); } int length = uncommonWords.size(); String[] uncommonWordsArray = new String[length]; for (int i = 0; i < length; i++) uncommonWordsArray[i] = uncommonWords.get(i); return uncommonWordsArray; } }
-
// OJ: https://leetcode.com/problems/uncommon-words-from-two-sentences/ // Time: O(A+B) // Space: O(A+B) class Solution { private: unordered_map<string, int> getCounts(string s) { istringstream ss(s); string word; unordered_map<string, int> m; while (ss >> word) m[word]++; return m; } public: vector<string> uncommonFromSentences(string A, string B) { auto m = getCounts(A), n = getCounts(B); vector<string> ans; for (auto &p : m) { if (p.second == 1 && n.find(p.first) == n.end()) ans.push_back(p.first); } for (auto &p : n) { if (p.second == 1 && m.find(p.first) == m.end()) ans.push_back(p.first); } return ans; } };
-
class Solution: def uncommonFromSentences(self, s1: str, s2: str) -> List[str]: cnt = Counter(s1.split()) + Counter(s2.split()) return [s for s, v in cnt.items() if v == 1] ############ 3 class Solution: def uncommonFromSentences(self, A, B): """ :type A: str :type B: str :rtype: List[str] """ count_A = collections.Counter(A.split(' ')) count_B = collections.Counter(B.split(' ')) words = list((count_A.keys() | count_B.keys()) - (count_A.keys() & count_B.keys())) ans = [] for word in words: if count_A[word] == 1 or count_B[word] == 1: ans.append(word) return ans