HackerRank Python - Company Logo

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 , which is the company name in lowercase letters, your task is to find the top three most common characters in the string.

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

  1. #!/bin/python3
  2. import math
  3. import os
  4. import random
  5. import re
  6. import sys
  7. import collections
  8. s = sorted(input().strip())
  9. s_counter = collections.Counter(s).most_common()
  10. s_counter = sorted(s_counter, key=lambda x: (x[1] * -1, x[0]))
  11. for i in range(0, 3):
  12. print(s_counter[i][0], s_counter[i][1])


Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube