# Hackerrank Solution: Company Logo in Python [3 Methods]

## Question: Company Logo - Hacker Rank (Python)

A newly opened multinational brand has decided to base its 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.

For example, according to the conditions described above,

GOOGLE would have its logo with the letters G, O, E.

Input Format:

A single line of input containing the string S.

Constraints:

• 3 < len(S) <104
• S has at least 3 distinct characters

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 of output:

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.

## Possible Solutions

Now, we will go through the possible solutions and explain each of them and the concepts that we will use in them: The question already has the following Python code added for you:

``````import math
import os
import random
import re
import sys
from collections import Counter

if __name__ == '__main__':
s = input()``````

So, we will add the following solutions, to the above code.

• Using the Counter function
• Using the Lambda function
• Using the Python Class

### Solution-1: Using the Counter function

We will first sort the string, then use the counter method to find the frequency of each of the words and finally, we will print the three words with their frequency as shown below:

``````# importing the modules
import math
import os
import random
import re
import sys
from collections import Counter

# using __name__ variable
if __name__ == '__main__':

# taking input from user and sorting it
s = input()
s = sorted(s)

# using counter method to find the frequency of each of the words
FREQUENCY = Counter(list(s))

# using for loop to print the three words with frequency
for k, v in FREQUENCY.most_common(3):
print(k, v)``````

Input:

``aabbbccde``

Output:

```b 3
a 2
c 2```

As you can see, we get the desired output as was provided in the explanation part.

### Solution-2: Using the Lambda function

In Python, a lambda function is a single-line function declared with no name, which can have any number of arguments, but it can only have one expression. We will now use the lambda function in our solution to get the desired result. We will use lambda to sort the characters along with their frequencies in a list in descending order and then will print the first three items.

``````# importing the required modules
import math
import os
import random
import re
import sys
from collections import Counter

# Using __name__ variable
if __name__ == '__main__':

# taking imput and then sorting
s = sorted(input().strip())

# finiding frequency
s_counter =Counter(s).most_common()

# using lambda function sort the items with frequencies in deceding order
s_counter = sorted(s_counter, key=lambda x: (x * -1, x))

# printing the first three items
for i in range(0, 3):
print(s_counter[i], s_counter[i])``````

Input:

``````aabbbccde
``````

Output:

```b 3
a 2
c 2```

As you can see, we were able to print the desired output.

### Solution-3: Using Python class

A python class is a code template for creating objects. Objects have member variables and have behavior associated with them. In python a class is created by the keyword class. An object is created using the constructor of the class. Instead of using the `__name__` variable, we will now use the Python class and write our Python code inside this class.

``````# importing the modules
from collections import Counter, OrderedDict

# creating python class
class OrderedCounter(Counter, OrderedDict):
pass

# creating new object in class each time we call in range of 3 , when the item is sorted
[print(*c) for c in OrderedCounter(sorted(input())).most_common(3)]``````

Input:

``aabbbccde``

Output:

```b 3
a 2
c 2```

As you can see, we are taking input from users, then sorting it and finding the top three most frequently occurring characters. At the same time, we are creating new objects by calling the class as well.

## Summary

In this short article, we discussed the Company logo question from Hacker Rank and we come up with multiple solutions. We were able to solve the questions using the counter method, lambda function, and Python class.

## References

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation. For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!