Count By First Letter
Python
Easy
2 views
Problem Description
Read n words. Count how many words start with each first letter. Output results sorted by letter as: letter count each on new line.
Input Format
First line n. Next n lines words.
Output Format
Multiple lines letter count.
Sample Test Case
Input:
5
apple
ant
bat
ball
cat
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines: sys.exit(0)
n=int(lines[0].strip())
mp={}
for i in range(1,1+n):
w=(lines[i] if i<len(lines) else '').strip()
if not w: continue
ch=w[0]
mp[ch]=mp.get(ch,0)+1
out=[]
for k in sorted(mp.keys()):
out.append(f'{k} {mp[k]}')
sys.stdout.write('\
'.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!