[LeetCode&Python] Problem 884. Uncommon Words from Two Sentences
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.
class Solution:
def uncommonFromSentences(self, A, B):
"""
:type A: str
:type B: str
:rtype: List[str]
"""
listA=A.split(' ')
listB=B.split(' ') ans=[] nA=len(listA)
nB=len(listB) for i in range(nA):
ch=listA[i]
if not ch in listB and not ch in (listA[:i]+listA[i+1:]):
ans.append(ch) for j in range(nB):
ch=listB[j]
if not ch in listA and not ch in (listB[:j]+listB[j+1:]):
ans.append(ch) return ans
[LeetCode&Python] Problem 884. Uncommon Words from Two Sentences的更多相关文章
- 【Leetcode_easy】884. Uncommon Words from Two Sentences
problem 884. Uncommon Words from Two Sentences 题意:只要在两个句子中单词出现总次数大于1次即可. 注意掌握istringstream/map/set的使 ...
- 【LeetCode】884. Uncommon Words from Two Sentences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 日期 题目地址:https://leetc ...
- [LeetCode] 884. Uncommon Words from Two Sentences 两个句子中不相同的单词
We are given two sentences A and B. (A sentence is a string of space separated words. Each word co ...
- LeetCode 884 Uncommon Words from Two Sentences 解题报告
题目要求 We are given two sentences A and B. (A sentence is a string of space separated words. Each wo ...
- [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode&Python] Problem 387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- [LeetCode&Python] Problem 427. Construct Quad Tree
We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...
- [LeetCode&Python] Problem 371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- [LeetCode&Python] Problem 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
随机推荐
- [设计模式]适配器模式Adapter
将一个类的接口转换成客户希望的另外一个接口. A d a p t e r模式使得原本 由于接口不兼容而不能一起工作的那些类可以一起工作.
- Caffe 学习系列
学习列表: Google protocol buffer在windows下的编译 caffe windows 学习第一步:编译和安装(vs2012+win 64) caffe windows学习:第一 ...
- Short Encoding of Words
2018-07-02 09:48:48 问题描述: 问题求解: 方法一.问题给了规模n = 2000,也就是说用BF在O(n^2)的时间复杂度可以过,因此,第一个方法就是BF,但是需要注意的是这里已经 ...
- 关于 [TNS-12516 TNS:listener could not find instance with matching protocol stack ]
Title: Intermittent TNS-12516 or TNS-12519 Errors Connecting Via Net Symptom(s) ~~~~~~~~~~ Client co ...
- jsp动作之 getProperty
getProperty就是用来获取(读取)实例化的内容的. 说明了就是(Techerobj实例为样,用name=张三,age=21等属性) <%=Techerobj.getName()%> ...
- Connected Components? CodeForces - 920E (bfs)
大意:给定无向图, 求补图的连通块数 bfs模拟即可, 这里用了map存图, set维护未划分的点集, 复杂度$O(nlog^2n)$, 用链表的话可以$O(n)$ #include <iost ...
- CSS单位分析
px:单位代表像素,1px代表一个像素点. %:设置子元素为父容器的占比. em:代表该元素中一个字体所占字符,常用在文字首行缩进.其具有继承性. rem:始终代表html中的字符所在位置. vm:1 ...
- UVA-10692 Huge Mods
题目大意:计算a1^a2^a3^a4......^an模m的值. 题目解析:幂取模运算的结果一定有周期.一旦找到周期就可把高次幂转化为低次幂.有降幂公式 (a^x)%m=(a^(x%phi(m)+ph ...
- React Js之组件(Component)与state
React Js组件: 组件(Component)是为了更好的维护我们的应用,可以在不影响其他的组件的情况下更新或者更改组件. state:是标记数据的来源,我们使state比较简单和单一,如果我们有 ...
- en笔记音标
清辅音和浊辅音区别 开音节和闭音节区别 1 2 3 4 5 6 7 a o e i u w y 开音节 /eɪ/ /əu/ /i:/ /aɪ/ Ju: /aɪ/ 闭音节 /æ/ /ɒ/ /ə/ / ...