This content originally appeared on DEV Community and was authored by Kathan Vakharia
The Question
You are given n words. Some words may repeat. For each word, output its number of occurrences. The output order should correspond with the input order of appearance of the word.
The Approach
- store the words as they come in the Counter.
- print them.
? I hope you remember,
Counter
after python 3.7 internally maintains insertion order.Code
from collections import Counter
words = list()
#n -> no of words
n = int(input())
for _ in range(n):
words.append(input())
#
c =Counter(words)
print(len(c))
print(*c.values())
This content originally appeared on DEV Community and was authored by Kathan Vakharia
Kathan Vakharia | Sciencx (2021-08-26T05:49:09+00:00) Python Collections: Hackerrank Question on Counter. Retrieved from https://www.scien.cx/2021/08/26/python-collections-hackerrank-question-on-counter/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.