Python program to find top three mostly occurred letters from company name

Sometimes we need to find the most frequently occurring characters in a company name. This program identifies the top three most common letters and displays them with their frequencies following specific sorting rules.

Problem Requirements

Given a company name as a string, we need to find the three most common characters by following these rules ?

  • Pick the most frequent three letters
  • Sort them in descending order by frequency
  • If frequencies are equal, sort alphabetically

For example, if the input is s = "TUTORIALSPOINT", the output should be [[3, 'T'], [2, 'I'], [2, 'O']].

Algorithm Steps

To solve this problem, we follow these steps ?

  • Create a frequency map of all letters in the string
  • Convert the frequency map to a list of [frequency, letter] pairs
  • Sort first alphabetically, then by frequency in descending order
  • Return the first three items

Implementation

from collections import Counter

def solve(s):
    # Count frequency of each character
    x = Counter(s)
    res = []
    
    # Create list of [frequency, character] pairs
    for i in x:
        res.append([x[i], i])
    
    # Sort alphabetically first (stable sort)
    res = sorted(res, key=lambda cnt: cnt[1])
    
    # Sort by frequency in descending order
    res = sorted(res, key=lambda cnt: cnt[0], reverse=True)
    
    # Return top 3
    return res[:3]

# Test with example
s = "TUTORIALSPOINT"
result = solve(s)
print("Company name:", s)
print("Top 3 frequent letters:", result)
Company name: TUTORIALSPOINT
Top 3 frequent letters: [[3, 'T'], [2, 'I'], [2, 'O']]

How It Works

Let's trace through the example "TUTORIALSPOINT" ?

from collections import Counter

s = "TUTORIALSPOINT"
char_count = Counter(s)
print("Character frequencies:")
for char, freq in sorted(char_count.items()):
    print(f"'{char}': {freq}")

print(f"\nMost frequent: T appears {char_count['T']} times")
print(f"Second most: I and O both appear {char_count['I']} times")
print("Since I comes before O alphabetically, I is ranked higher")
Character frequencies:
'A': 1
'I': 2
'L': 1
'N': 1
'O': 2
'P': 1
'R': 1
'S': 1
'T': 3
'U': 1

Most frequent: T appears 3 times
Second most: I and O both appear 2 times
Since I comes before O alphabetically, I is ranked higher

Alternative Approach

Here's a more concise version using a single sort with multiple keys ?

from collections import Counter

def solve_alternative(s):
    char_count = Counter(s)
    
    # Sort by frequency (descending) then alphabetically (ascending)
    sorted_chars = sorted(char_count.items(), key=lambda x: (-x[1], x[0]))
    
    # Convert to required format and take top 3
    return [[freq, char] for char, freq in sorted_chars[:3]]

s = "TUTORIALSPOINT"
result = solve_alternative(s)
print("Alternative approach result:", result)
Alternative approach result: [[3, 'T'], [2, 'I'], [2, 'O']]

Testing with Different Inputs

def solve(s):
    from collections import Counter
    x = Counter(s)
    res = []
    
    for i in x:
        res.append([x[i], i])
    
    res = sorted(res, key=lambda cnt: cnt[1])
    res = sorted(res, key=lambda cnt: cnt[0], reverse=True)
    
    return res[:3]

# Test with different company names
test_cases = ["GOOGLE", "MICROSOFT", "AMAZON"]

for company in test_cases:
    result = solve(company)
    print(f"{company}: {result}")
GOOGLE: [[2, 'G'], [2, 'O'], [1, 'E']]
MICROSOFT: [[2, 'O'], [2, 'R'], [1, 'C']]
AMAZON: [[2, 'A'], [1, 'A'], [1, 'M']]

Conclusion

This algorithm efficiently finds the top three most frequent characters using Counter for frequency calculation and stable sorting for proper ordering. The two-step sorting ensures characters with equal frequencies are ordered alphabetically while maintaining frequency-based descending order.

Updated on: 2026-03-26T15:40:25+05:30

658 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements