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.
Did all the work in one line:
s = Counter(sorted(list(input()))).most_common(3)
Still needed two lines to print the output:
for i in s:
print(i[0], i[1])
But basically the line takes the input(), puts the letters in a list, sorts them, then makes them a counter and takes the top 3 most common.
Because the Counter takes the first from the list if the counts match, it is in alphabetical order as it counts.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Company Logo
You are viewing a single comment's thread. Return to all comments →
Did all the work in one line: s = Counter(sorted(list(input()))).most_common(3)
Still needed two lines to print the output: for i in s: print(i[0], i[1])
But basically the line takes the input(), puts the letters in a list, sorts them, then makes them a counter and takes the top 3 most common. Because the Counter takes the first from the list if the counts match, it is in alphabetical order as it counts.