0Pricing
Coding Interview Prep · Lesson

Count Letters with a Frequency Table

Tally characters using a dict or array.

Count Letters with a Frequency Table is a free Coding Interview Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Count Characters

Tons of string problems boil down to one question: how often does each character appear? A frequency table answers that in one pass. 📊

The Dictionary Way

A plain dict maps each character to its count. It works for any alphabet, including unicode and symbols.

freq = {}
for ch in 'apple':
    freq[ch] = freq.get(ch, 0) + 1
print(freq)

get() Avoids KeyError

Using get(ch, 0) returns 0 when the key is missing, so the first time you see a letter it starts cleanly at one.

Counter Does It For You

The Counter class from collections builds the whole table in a single line. It is the contest favorite for speed and clarity.

from collections import Counter
freq = Counter('apple')
print(freq['p'])  # 2

Missing Keys Return Zero

A Counter never raises on a missing key. Asking for a letter you never saw just gives 0, which keeps your code branch-free.

from collections import Counter
c = Counter('abc')
print(c['z'])  # 0

The Fixed Array Way

For lowercase letters only, a 26-slot list is even faster. Index each letter with ord math and bump the count.

cnt = [0] * 26
for ch in 'apple':
    cnt[ord(ch) - ord('a')] += 1

Array vs Dict Tradeoff

The array is fastest but only fits a known small alphabet. The dict or Counter handles any characters at a tiny cost.

Find the Most Common

Counter gives you most_common(k), which returns the top k characters already sorted by frequency. No manual sorting needed.

from collections import Counter
print(Counter('mississippi').most_common(1))

Compare Two Tables

Two strings are anagrams exactly when their frequency tables match. Comparing two Counters is a one-liner.

from collections import Counter
print(Counter('listen') == Counter('silent'))  # True

One Pass Is Enough

Building the table is O(n), a single scan of the string. After that, every lookup is constant time.

Subtract to Find Surplus

Counters support subtraction, so you can spot which characters one string has that another lacks. Great for ransom-note style tasks.

from collections import Counter
print(Counter('aabb') - Counter('ab'))

Quick Check

One question on counting characters.

Recap

You can now tally characters with a dict, a Counter, or a 26-slot array, and use those tables to test anagrams and find frequent letters. 🎉

Frequently asked questions

Is the “Count Letters with a Frequency Table” lesson free?

Yes — the full text of “Count Letters with a Frequency Table” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.

What will I learn in “Count Letters with a Frequency Table”?

Tally characters using a dict or array. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Coding Interview Prep?

No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Count Letters with a Frequency Table” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Coding Interview Prep lesson?

Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Characters, ord & chr Tricks
  2. Count Letters with a Frequency Table
  3. Palindrome Checks Done Right
  4. Split, Strip & Rejoin Words
← Back to Coding Interview Prep