In this HackerRank Company Logo problem solution, A newly opened multinational brand has decided to base their company logo on the three most common characters in the company name. They are now trying out various combinations of company names and logos based on this condition. Given a string S, which is the company name in lowercase letters, your task is to find the top three most common characters in the string.

  1. Print the three most common characters along with their occurrence count.
  2. Sort in descending order of occurrence count.
  3. If the occurrence count is the same, sort the characters in alphabetical order.

HackerRank Company Logo in python problem solution

HackerRank Company Logo in python problem solution

from operator import itemgetter, attrgetter, methodcaller

S = raw_input()

myDict = {}
for c in S:
    if c in myDict:
        myDict[c] += 1
    else:
        myDict[c] = 1

myList = []
for v, k, in myDict.iteritems():
    myList.append([v, -k])

newList = sorted(myList, key=itemgetter(1,0))

print newList[0][0], -newList[0][1]
print newList[1][0], -newList[1][1]
print newList[2][0], -newList[2][1]