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())
Top comments (2)
That was smart! :-)
Thanks!π