Input Format
A single line of input containing the string .
Constraints
Output Format
Print the three most common characters along with their occurrence count each on a separate line.
Sort output in descending order of occurrence count.
If the occurrence count is the same, sort the characters in alphabetical order.

hackerrank company logo problem solution in python

Problem solution in python 2 programming language.

S = raw_input()
letters = [0]*26

for letter in S:
    letters[ord(letter)-ord('a')] += 1

for _ in range(3):
    
    max_letter = max(letters)
    
    for index in range(26):
        if max_letter == letters[index]:
            print chr(ord('a')+index), max_letter
            letters[index] = -1
            break

Second solution

from collections import Counter
from operator import itemgetter

for item in (sorted(sorted(Counter(raw_input()).items()), key = itemgetter(1), reverse = True)[:3]):
    print item[0], item[1]

Third solution

from collections import Counter

for letter, counts in sorted(Counter(raw_input()).most_common(),key = lambda x:(-x[1],x[0]))[:3]:
    print letter, counts

Problem solution in python 3 programming language.

S = input()
letters = [0]*26

for letter in S:
    letters[ord(letter)-ord('a')] += 1

for _ in range(3):
    
    max_letter = max(letters)
    
    for index in range(26):
        if max_letter == letters[index]:
            print(chr(ord('a')+index), max_letter)
            letters[index] = -1
            break

Second solution

from collections import Counter
from operator import itemgetter

for item in (sorted(sorted(Counter(raw_input()).items()), key = itemgetter(1), reverse = True)[:3]):
    print(item[0], item[1])

Third solution

from collections import Counter

for letter, counts in sorted(Counter(raw_input()).most_common(),key = lambda x:(-x[1],x[0]))[:3]:
    print letter, counts