Frequency of Frequencies
Python
Hard
3 views
Problem Description
Read n words. Build frequency of each word. Then q queries of integer f. For each query, output how many distinct words have frequency exactly f.
Input Format
First line n. Next n lines words. Next line q. Next q lines f.
Output Format
q lines answers.
Sample Test Case
Input:
6
a
a
b
c
c
c
3
1
2
3
Constraints
Total characters
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines: sys.exit(0)
i=0
n=int(lines[i].strip()); i+=1
mp={}
for _ in range(n):
w=(lines[i] if i<len(lines) else '').strip(); i+=1
mp[w]=mp.get(w,0)+1
freqCount={}
for c in mp.values():
freqCount[c]=freqCount.get(c,0)+1
q=int(lines[i].strip()) if i<len(lines) else 0
i+=1
out=[]
for _ in range(q):
f=int((lines[i] if i<len(lines) else '0').strip()); i+=1
out.append(str(freqCount.get(f,0)))
sys.stdout.write('\
'.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!