Company Logo in python - HackerRank Solution

Company Logo in python - HackerRank Solution
Company Logo in python - HackerRank Solution


Problem :


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.
  • 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.
For example, according to the conditions described above,
GOOGLE would have it's logo with the letters G, O, E.



Input Format :

A single line of input containing the string S.

Constraints :

  • 3 < len(S) <= 10^4

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.



Sample Input :

aabbbccde

Sample Output :

b 3
a 2
c 2

Explanation :

aabbbccde
Here, b occurs 3 times. It is printed first.
Both a and c occur 2 times. So, a is printed in the second line and c in the third line because a comes before c in the alphabet.
Note: The string S has at least 3 distinct characters.



Solution :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Company Logo in python - Hacker Rank Solution
# python 3
#!/bin/python3
# Company Logo in python - Hacker Rank Solution START
import math
import os
import random
import re
import sys
import collections

if __name__ == '__main__':
    s = sorted(input().strip())
    s_counter = collections.Counter(s).most_common()
    s_counter = sorted(s_counter, key=lambda x: (x[1] * -1, x[0]))
    for i in range(0, 3):
        print(s_counter[i][0], s_counter[i][1])
# Company Logo in python - Hacker Rank Solution END





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
9 Comments
  • Abhiram Makam
    Abhiram Makam Saturday, September 11, 2021

    What is the need of line 15 here?

    • BlueGreenOlga
      BlueGreenOlga Wednesday, March 30, 2022

      Hi :) I actually tried the code without it and it works perfectly fine! I don't think you need it, as you already sort the letters of the string in line 13. However, I am just beginning to teach myself coding, so I am not 100% sure what that line should mean anyways. Hope this helps!

    • Anonymous
      Anonymous Saturday, July 02, 2022

      sort when s_counters has 2 or more chars have same count.

  • bhanu
    bhanu Thursday, December 09, 2021

    Nice Article. thanks for sharing this content
    Best Python Training Online
    Python Online Classes

  • Certifications and Career in Insurance
    Certifications and Career in Insurance Wednesday, February 09, 2022

    branding solution
    iVista is a creative and digital marketing agency providing best solutions in design and digital marketing for growing brands. We focus on driving results.

  • Anonymous
    Anonymous Tuesday, May 03, 2022

    Without importing those libraries it can be done this way:
    if __name__ == '__main__':
    x={}
    k=[]
    p=[]
    m=[]
    a=[]
    b=[]
    s = input()
    l=list(s)
    for i in l:
    if i not in k:
    k.append(i)
    for i in k:
    q=l.count(i)
    p.append(q)
    for i in range(len(k)):
    x[k[i]]=p[i]

    y=sorted(x)
    for i in y:
    a.append(i)
    b.append(x[i])
    for i in range(0,3):
    print(a[b.index(max(b))],b[b.index(max(b))])
    b[b.index(max(b))]=0

    • Anonymous
      Anonymous Thursday, August 04, 2022

      Can you explain; I got lost there?

  • Stevennall
    Stevennall Monday, May 09, 2022

    Thank you for taking the time to write such an informative post. Your blog is not only informative, but it is also very creative.
    designing your own logo

  • Anonymous
    Anonymous Friday, August 05, 2022

    I used this, without using the above libraries, but its failing 3/6 test...can some explain why? (I dont have the testcases unlocked so I cant see the reason)
    CODE -

    def func (s):
    s_low = s.lower()
    dic = {}
    for char in s_low:
    if char not in dic:
    dic[char] = 0
    dic[char]+=1
    Keymax_1 = max(dic, key= lambda x: dic[x])
    print(f'{Keymax_1} {dic[Keymax_1]}')
    del dic[Keymax_1]
    Keymax_2 = max(dic, key= lambda x: dic[x])
    print(f'{Keymax_2} {dic[Keymax_2]}')
    del dic[Keymax_2]
    Keymax_3 = max(dic, key= lambda x: dic[x])
    print(f'{Keymax_3} {dic[Keymax_3]}')

Add Comment
comment url