Question:
You are given n
words. Some words may repeat. For each word, print its number of occurrences. The output order should correspond with the input order of appearance of the word.
Sample Input:
4
bcdef
abcdefg
bcde
bcdef
Sample Output
3
2 1 1
Here's what I came up with:
n = int(input())
array = []
elements = {}
for index in range(n):
value = input()
if value not in array:
array.append(value)
elements[value] = 1
else:
elements[value] += 1
print(len(elements))
print(*(i for i in elements.values()), end=' ')
I stress tested it on Try it Online with the random string generator and found the runtime to be around 1.98s. But I'm getting TLE on the coding platform. How do I improve the speed (bit offtopic - is there any other approach)?