We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
n = int(input()) # Number of words
l = [] # List to store the words
seen = {} # Dictionary to keep track of occurrences
for i in range(n):
word = input() # Input the word
l.append(word) # Append word to list
if word in seen:
seen[word] += 1 # Increment count if word is already in the dictionary
else:
seen[word] = 1 # Initialize count to 1 if it's the first occurrence of the word
result = []
for word in l:
if seen.get(word) is not None:
result.append(seen[word]) # Append the count of occurrences
del seen[word] # Remove the word from the dictionary to avoid duplicates in output
print(len(result)) # Print the number of distinct words
print(*result) # Print the counts of occurrences in the order of appearance
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Word Order
You are viewing a single comment's thread. Return to all comments →
Dont crtl c/v ! Read and pls try to comprehend
n = int(input()) # Number of words l = [] # List to store the words seen = {} # Dictionary to keep track of occurrences
for i in range(n): word = input() # Input the word l.append(word) # Append word to list if word in seen: seen[word] += 1 # Increment count if word is already in the dictionary else: seen[word] = 1 # Initialize count to 1 if it's the first occurrence of the word
result = [] for word in l: if seen.get(word) is not None: result.append(seen[word]) # Append the count of occurrences del seen[word] # Remove the word from the dictionary to avoid duplicates in output
print(len(result)) # Print the number of distinct words print(*result) # Print the counts of occurrences in the order of appearance