1. 程式人生 > >884. Uncommon Words from Two Sentences 用collections模組裡的Counter類物件計數,返回字典中的key

884. Uncommon Words from Two Sentences 用collections模組裡的Counter類物件計數,返回字典中的key

We are given two sentences A and B.  (A sentence is a string of space separated words.  Each wordconsists 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"]
class Solution:
    def uncommonFromSentences(self, A, B):
        """
        :type A: str
        :type B: str
        :rtype: List[str]
        """
        c=collections.Counter((A + ' ' + B).split(' '))
        for k,v in c.items():
            if v == 1:
                return k

reference: 

collections.Counter

https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001411031239400f7181f65f33a4623bc42276a605debf6000

集合的方法:

http://www.runoob.com/python3/python3-set.html

字典的方法:

http://www.runoob.com/python/python-dictionary.html