4.6.1 · D3Theory of Computation

Worked examples — Alphabet, string, language — formal definitions

2,748 words12 min readBack to topic

The scenario matrix

Before solving anything, let's map the terrain. Every problem about alphabets/strings/languages lands in one of these cells. Think of it as a checklist: if we cover every row, we've covered the topic.

Cell Case class The tricky thing it tests Example
C1 Counting strings of a length multiplication rule , and E1
C2 The three "empties" telling , , apart E2
C3 Concatenation with inside a language why but E3
C4 Language star with degenerate inputs , E4
C5 Length / reversal algebra , , E5
C6 Counting a whole language up to a bound summing , geometric growth E6
C7 Real-world word problem modelling something as strings/language E7
C8 Countability twist (exam favourite) countable vs uncountable E8
C9 Prefix / suffix / substring counting positions & off-by-one edge cases E9

Rows C1–C9 are every "cell". The nine examples below hit them one-to-one. Cases like "single-symbol alphabet" and "the empty string as an input" appear inside several examples so degenerate inputs never go untested.


E1 — Counting strings of an exact length (cell C1)

Naive-answer trap
Saying because "there are no symbols." There is one string — the blank one — so the count is .

E2 — The three empties, side by side (cell C2)


E3 — Concatenation when a language contains (cell C3)


E4 — Star of degenerate languages (cell C4)


E5 — Length & reversal algebra (cell C5)


E6 — Counting a whole language up to a bound (cell C6)


E7 — Real-world word problem (cell C7)


E8 — The countability twist (cell C8)


E9 — Prefix / suffix / substring counting (cell C9)


Did we cover every cell?

We promised an exhaustive stress test of C1–C9. Here is the final tally:

Cell Covered by Key takeaway
C1 E1 , and gives 1
C2 E2 / / live on different floors
C3 E3 ,
C4 E4 , never
C5 E5 lengths add; reversal swaps order & pieces
C6 E6 disjoint sizes add:
C7 E7 model as a language, then subtract:
C8 E8 countable, uncountable
C9 E9 prefixes/suffixes; dedupe substrings → 9

Every row of the scenario matrix now has a fully worked example, including the degenerate inputs (, , ) threaded through several of them. No case class is left untested.


Recall Quick self-test (try before opening)

Q1 — for any alphabet? ::: (the single empty string ). Q2 — Value of ? ::: , because is always included. Q3 — Is the set of all languages over countable? ::: No — it is , uncountable. Q4 — Accepted PINs in E7? ::: . Q5 — What does shortlex order mean? ::: Sort by length first, then lexicographically within each length.