[LeetCode]#2085. Count Common Words With One Occurrence

Input: words1 = ["leetcode","is","amazing","as","is"], words2 = ["amazing","leetcode","is"]
Output: 2
Explanation:
- "leetcode" appears exactly once in each of the two arrays. We count this string.
- "amazing" appears exactly once in each of the two arrays. We count this string.
- "is" appears in each of the two arrays, but there are 2 occurrences of it in words1. We do not count this string.
- "as" appears once in words1, but does not appear in words2. We do not count this string.
Thus, there are 2 strings that appear exactly once in each of the two arrays.
  1. Get intersection between words1 and words2
  2. Find is there only one string in words1 and words2 based on step1 result.
  3. return string number
class Solution:
def countWords(self, words1, words2):
same_w=list(set(words1) & set(words2))
ans=0
for i in same_w:
if words1.count(i)==1 and words2.count(i)==1:
ans+=1
return ans

--

--

--

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Flutter: Creating Custome ListView with Map List

Let’s destroy some MVVM+C

The Secret Mozilla Hubs Master Plan

Difference between session and cookies in PHP

It’s done! Or is it?

SeleniumWebTesting with Ruby

MySQL: What to Learn: SQL Statement Syntax

Text anonymization with Presidio and Faker

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

[LeetCode]#2114. Maximum Number of Words Found in Sentences

LeetCode Patterns Adventure 12 — Palindrome Linked List

Implementing Linked List Operations in Python

Heaps, the python way

heap