Intersection of Keys
Python
Medium
3 views
Problem Description
Read two dictionaries A and B as lists of keys. Output how many keys are present in both.
Input Format
Line1: n. Next n lines keys of A. Next line: m. Next m lines keys of B.
Output Format
One integer commonCount.
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines: sys.exit(0)
i=0
n=int(lines[i].strip()); i+=1
A={}
for _ in range(n):
k=(lines[i] if i<len(lines) else '').strip(); i+=1
if k!='':
A[k]=1
m=int(lines[i].strip()) if i<len(lines) else 0
i+=1
cnt=0
for _ in range(m):
k=(lines[i] if i<len(lines) else '').strip(); i+=1
if k in A:
cnt+=1
sys.stdout.write(str(cnt))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!