博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
884.Uncommon Words from Two Sentences
阅读量:5364 次
发布时间:2019-06-15

本文共 1361 字,大约阅读时间需要 4 分钟。

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:

  1. 0 <= A.length <= 200
  2. 0 <= B.length <= 200
  3. A and B both contain only spaces and lowercase letters.
class Solution:    def uncommonFromSentences(self, A, B):        """        :type A: str        :type B: str        :rtype: List[str]        """        dicta = {}        dictb = {}        res = []        for i in A.split(' '):            if i not in dicta:                dicta[i] = 1            else:                dicta[i] += 1        for i in B.split(' '):            if i not in dictb:                dictb[i] = 1            else:                dictb[i] += 1        for key,value in dicta.items():            if value==1 and key not in dictb:                res.append(key)        for key,value in dictb.items():            if value==1 and key not in dicta:                res.append(key)        return res

转载于:https://www.cnblogs.com/bernieloveslife/p/9755385.html

你可能感兴趣的文章
iOS解决隐藏导航栏后,打开照片选择器后导航栏不显示的问题以及更换导航栏背景色...
查看>>
bzoj 2632 [neerc2011]Gcd guessing game——贪心(存疑)
查看>>
不同语言的内存管理
查看>>
05BeautifulSoup遍历文档书及搜索文档树
查看>>
e3mall商城的归纳总结1之项目的架构
查看>>
CF798
查看>>
Codeforces 294C 组合数学
查看>>
网页上播放mp3或flash
查看>>
MapReduce教程(一)基于MapReduce框架开发<转>
查看>>
洛谷P1829 [国家集训队]Crash的数字表格 / JZPTAB(莫比乌斯反演)
查看>>
关于字符串为空的判断条件
查看>>
使用sysprep.exe制作GHO或WIM镜像
查看>>
netstat命令的使用详解
查看>>
[LeetCode#72]Edit Distance
查看>>
.NET备份博客园随笔分类文章
查看>>
Ubuntu窗口大小调节方法
查看>>
English Learning -->英语词汇记忆10大规则<思维导图>
查看>>
HDOJ(HDU) 2148 Score(比较、)
查看>>
大不了高三艹个FZU
查看>>
S2_SQL_第一章
查看>>