[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

Building an online marketplace from scratch — the dark side of Salesforce limits

VMware Interview Questions

DevOps And Its Significance

ASP.NET Core Performance Tuning

Thinkium Developer Bounty Plan

Learning How To Classify Images Using Keras Part 3

Surfacing OPEN content by using GitHub and YouTube APIs (Process & Architecture)

Css Border Width 3 Values Of Critical Thinking

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]#2000. Reverse Prefix of Word

LeetCode has a Medium coding Problem in Its’ Algorithm Section “Regular Expression Matching…

LeetCode Patterns Adventure 3— Find All Numbers Disappeared in an Array

Python Interview Questions for 2022