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.
here it is, sorry for the horrible formatting, but I do not see how to improve it.
Essentially I use a dictionnary to collect occurrences of each word. I also store on each dictionnary entry the first occurrence of each word.
at a second phase I initialize an empty list with as many elements as words were found during the furst phase.
Lastly, I move the dictionnary contents to the list, making it ordered based on first occurrence of each word.
The whole algorithm takes O(2n) to run
n = int(raw_input())
wc = 0
d = dict()
""" d = [occurences , word count position of first occurence] wc increases with every NEW and DIFFERENT word"""
for x in range(n):
w = raw_input()[:-1]
if not(d.has_key(w)):
d[w]=[1,wc]
wc = wc + 1
else:
d[w][0] = d[w][0] + 1
"""initializing response list"""
res = [["",0] for x in range(wc)]
"""translate dictionarry into list, with first occurring words first in the list"""
for el in d:
"""el has the dict key string"""
res[d[el][1]] = [el,d[el][0]]
"""output, the extra space at the end is to accomodate for an extra space in the output test case files"
print wc
for el in res:
print el[1],
print " "
print
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Word Order
You are viewing a single comment's thread. Return to all comments →
Sure,
here it is, sorry for the horrible formatting, but I do not see how to improve it. Essentially I use a dictionnary to collect occurrences of each word. I also store on each dictionnary entry the first occurrence of each word.
at a second phase I initialize an empty list with as many elements as words were found during the furst phase.
Lastly, I move the dictionnary contents to the list, making it ordered based on first occurrence of each word.
The whole algorithm takes O(2n) to run