赞
踩
给你两个字符串数组 words1 和 words2 ,请你返回在两个字符串数组中 都恰好出现一次 的字符串的数目。
示例 1:
输入:words1 = [“leetcode”,“is”,“amazing”,“as”,“is”], words2 = [“amazing”,“leetcode”,“is”]
输出:2
解释:
分别统计两个words里字符串的频率,然后遍历两个频率表里公共的部分,查找频率都为1的字符串。
class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
words1=Counter(words1)
words2=Counter(words2)
count=0
for i in words1.keys()&words2.keys():
if words1[i]==1 and words2[i]==1:
count+=1
return count
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。