livepython.net

207. Python Program to Find the Most Common Character in a String

MediumPhase 2 · Practice

This one takes a few steps. It uses a for loop, if and a dictionary, and you know all three.

Overview

Finding the most common character is a common interview question about strings. Code breakers count characters in the same way.

Problem Statement

Rosa writes short messages to friends. Rosa wants to know which character appears most often in a message.

  • Spaces do not count. Every other character counts: letters, digits and signs like , or ..
  • Capital and small letters are different: A and a are counted apart.
  • If two characters have the same biggest count, the one that appears first in the text wins.
  • Print two lines: Character: <character> and Count: <number>.

Examples

Example 1. Input hello world gives:

Character: l
Count: 3

Why: l appears 3 times: twice in hello and once in world. o appears only 2 times.

Example 2. Input Mississippi gives:

Character: i
Count: 4

Why: i and s both appear 4 times. i appears first in the text, right after M, so i wins.

Input

One line of text, with at least one character that is not a space. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

Two lines: Character: <character> and Count: <number>. The tests try 8 different inputs, also ties and capital letters.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 13 · Dictionaries
solution.py
# write your code here

EXPECTED OUTPUT

1Character: l
2Count: 3

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.