G27
Self-checkedTier 1 10 min readtoc.alphabets-strings-languages

Alphabets, strings and languages

The vocabulary of the whole subject: alphabets, strings, the empty string, concatenation and powers, prefixes, suffixes, substrings and subsequences, Sigma-star, languages as sets of strings and the operations on them (union, intersection, complement, concatenation, Kleene star, reversal), the special cases of the empty language and the language of the empty string, and counting.

Why it exists

Computer science asks what can be computed and how hard it is. To say anything exact, a problem has to become a mathematical object. The object is a language: a set of strings. "Is this number prime?" becomes "is this string, the binary form of the number, in the language of primes?". "Is this program syntactically correct?" is membership in the language of valid programs. Every machine of this subject (finite automata, pushdown automata, Turing machines) is a device that decides, or recognises, membership in a language, and every grammar generates one. This page fixes the notation, which GATE uses without explanation in every later question.

Prerequisites

None. This is a starting point.

Nothing: sets and functions from school mathematics are enough.

Precise definitions

Alphabet. A finite, non-empty set Σ\Sigma of symbols (letters, digits, bits). Examples: {0,1}\{0, 1\}, {a,b}\{a, b\}, the ASCII characters.

String (word). A finite sequence of symbols of Σ\Sigma. The length ∣w∣|w| is the number of symbols. The empty string ε\varepsilon has length 00 (it is a string, not a symbol, and not the empty set). ∣w∣a|w|_a denotes the number of occurrences of the symbol aa in ww.

Concatenation. xyxy is xx followed by yy; ∣xy∣=∣x∣+∣y∣|xy| = |x| + |y|. It is associative ((xy)z=x(yz)(xy)z = x(yz)), has the identity ε\varepsilon (εw=wε=w\varepsilon w = w \varepsilon = w), and is not commutative (ab≠baab \ne ba). wnw^n is ww concatenated nn times, w0=εw^0 = \varepsilon.

Σn\Sigma^n and Σ∗\Sigma^*. Σn\Sigma^n is the set of all strings of length nn; Σ∗=Σ0∪Σ1∪Σ2∪⋯\Sigma^* = \Sigma^0 \cup \Sigma^1 \cup \Sigma^2 \cup \cdots is the set of all strings (including ε\varepsilon), and Σ+=Σ∗∖{ε}\Sigma^+ = \Sigma^* \setminus \{\varepsilon\}. For ∣Σ∣=k|\Sigma| = k: ∣Σn∣=kn|\Sigma^n| = k^n. Σ∗\Sigma^* is infinite but countable: its strings can be listed in order of length, and alphabetically within a length (shortlex order).

Parts of a string. xx is a prefix of ww if w=xyw = xy for some yy; a suffix if w=yxw = yx; a substring (factor) if w=yxzw = yxz (a contiguous piece); a subsequence if its symbols appear in ww in the same order but not necessarily next to each other. Every prefix, suffix and substring is a subsequence; the converse is false.

Language. Any subset L⊆Σ∗L \subseteq \Sigma^*. It may be finite or infinite. Examples over {0,1}\{0, 1\}: the binary strings that end with 0101; the strings with equally many 00s and 11s; the binary representations of prime numbers.

Operations on languages (A,B⊆Σ∗A, B \subseteq \Sigma^*):

  • union A∪BA \cup B, intersection A∩BA \cap B, difference A∖BA \setminus B;
  • complement A‾=Σ∗∖A\overline{A} = \Sigma^* \setminus A (it depends on the alphabet);
  • concatenation AB={xy:x∈A, y∈B}AB = \{xy : x \in A,\ y \in B\} and powers A0={ε}A^0 = \{\varepsilon\}, An+1=AnAA^{n+1} = A^n A;
  • Kleene star A∗=A0∪A1∪A2∪⋯A^* = A^0 \cup A^1 \cup A^2 \cup \cdots (any number of strings of AA, including none), and A+=AA∗=A1∪A2∪⋯A^+ = A A^* = A^1 \cup A^2 \cup \cdots;
  • reversal AR={wR:w∈A}A^R = \{w^R : w \in A\}.

Decision problems and languages. A yes/no problem on inputs written as strings is the language of the inputs whose answer is yes. "Deciding" or "recognising" the problem means deciding or recognising the language.

How it works

Counting strings and languages

For an alphabet of kk symbols there are exactly knk^n strings of length nn and (kn+1−1)/(k−1)(k^{n+1} - 1)/(k - 1) strings of length at most nn (for k=2k = 2: 2n+1−12^{n+1} - 1). A language is a set of strings, so it can be counted length by length. The table gives, for languages over {0,1}\{0, 1\}, how many strings of each length belong to each language; each number was found by running a machine for the language on all strings of that length and was confirmed by a second method (a dynamic programme over the machine's states).

How many strings of each length are in a language: start

Computed by a script, not typed
length nall stringsends with 01contains 00has an even number of 1slength divisible by 3
0
1
2
3
4
5
6
7

Step 0

Languages over {0, 1}. The counts come from running a DFA for each language on every string (and, independently, from a dynamic program over the states). A language is a SET of strings, so it can be counted length by length.

0 / 8
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc-traces.py

The first column is 2n2^n; "ends with 0101" has 2n−22^{n-2} strings of length n≥2n \ge 2 (fix the last two symbols); "has an even number of 11s" has 2n−12^{n-1} strings of each length n≥1n \ge 1 (half of all strings); "length divisible by 3" has 2n2^n strings when nn is a multiple of 33 and none otherwise.

A string with nn symbols has:

  • exactly n+1n + 1 prefixes and n+1n + 1 suffixes (counting ε\varepsilon and the string itself);
  • at most n(n+1)/2+1n(n+1)/2 + 1 distinct substrings (one for each pair of end positions, plus ε\varepsilon), with equality when all symbols are different; the string abcabc has 77, the string aaaaaa only 44 (ε\varepsilon, aa, aaaa, aaaaaa);
  • at most 2n2^n distinct subsequences (one for each subset of positions), again with equality when all symbols are different (abcabc has 88).

Languages are uncountably many. Σ∗\Sigma^* is countable, but the set of all subsets of Σ∗\Sigma^* (all languages) is uncountable (Cantor's diagonal argument). Machines and grammars are finite descriptions, and there are only countably many of those. So most languages have no finite description at all, and in particular most languages cannot be recognised by any program. This is why the limits of computation (undecidability) exist.

The operations in practice

Take A={a,ab}A = \{a, ab\} and B={b,ε}B = \{b, \varepsilon\}. Then AB={ab,a,abb,ab}={a,ab,abb}AB = \{ab, a, abb, ab\} = \{a, ab, abb\}: three strings, not four, because a⋅ba \cdot b and ab⋅εab \cdot \varepsilon coincide. In general ∣AB∣≤∣A∣∣B∣|AB| \le |A||B|. For L={a,bb}L = \{a, bb\} the star L∗L^* contains ε\varepsilon, aa, aaaa, bbbb, aaaaaa, abbabb, bbabba, and so on: the strings that can be cut into pieces aa and bbbb; the number of such strings of length nn is 1,1,2,3,5,8,13,…1, 1, 2, 3, 5, 8, 13, \ldots (a Fibonacci number: the string either ends with aa or with bbbb).

The empty language and the empty string are different and exam questions use the difference:

ExpressionValue
∅\emptysetthe empty set: no strings at all
{ε}\{\varepsilon\}the language with exactly one string, the empty one
L∅=∅LL \emptyset = \emptyset L∅\emptyset (nothing to concatenate)
L{ε}={ε}LL\{\varepsilon\} = \{\varepsilon\}LLL
∅∗\emptyset^*{ε}\{\varepsilon\} (zero concatenations)
∅+\emptyset^+∅\emptyset
L∗L^*always contains ε\varepsilon; L+L^+ contains ε\varepsilon only if LL does
(L∗)∗(L^*)^*L∗L^*
{ε}∗\{\varepsilon\}^*{ε}\{\varepsilon\}

Also: (A∪B)∗(A \cup B)^* is not A∗∪B∗A^* \cup B^* (the first contains abab for A={a}A = \{a\}, B={b}B = \{b\}, the second does not); A∪B‾=A‾∩B‾\overline{A \cup B} = \overline{A} \cap \overline{B} (De Morgan); (AB)R=BRAR(AB)^R = B^R A^R (the order reverses).

Worked examples

EX 1Count the strings

Easy

Over {a,b}\{a, b\}: strings of length exactly 44: 24=162^4 = 16. Of length at most 44: 1+2+4+8+16=31=25−11 + 2 + 4 + 8 + 16 = 31 = 2^5 - 1. The number of strings of length 55 that start with aa and end with bb: the first and last symbols are fixed, three are free: 23=82^3 = 8. Strings of length 66 with exactly two aas: (62)=15\binom{6}{2} = 15.

EX 2Substrings and subsequences

Easy

w=ababw = abab: prefixes ε,a,ab,aba,abab\varepsilon, a, ab, aba, abab (55); suffixes ε,b,ab,bab,abab\varepsilon, b, ab, bab, abab (55); distinct substrings: ε,a,b,ab,ba,aba,bab,abab\varepsilon, a, b, ab, ba, aba, bab, abab (88, which is less than 4⋅5/2+1=114 \cdot 5/2 + 1 = 11 because letters repeat). The string aaaa is a subsequence of abababab (positions 11 and 33) but not a substring.

EX 3Evaluate language expressions

Medium

L1={ab,b}L_1 = \{ab, b\}, L2={ε,a}L_2 = \{\varepsilon, a\}. L1L2={ab,aba,b,ba}L_1 L_2 = \{ab, aba, b, ba\}. L2∗={a}∗={ε,a,aa,…}L_2^* = \{a\}^* = \{\varepsilon, a, aa, \ldots\} (since ε\varepsilon adds nothing). L1R={ba,b}L_1^R = \{ba, b\}. L1∩L2=∅L_1 \cap L_2 = \emptyset. L1∅=∅L_1 \emptyset = \emptyset but L1{ε}=L1L_1 \{\varepsilon\} = L_1.

EX 4The complement depends on the alphabet

Medium

Let L={w∈{a,b}∗:w contains aa}L = \{w \in \{a, b\}^* : w \text{ contains } aa\}. Then L‾\overline{L} over {a,b}\{a, b\} is the set of strings with no two consecutive aas. Over the larger alphabet {a,b,c}\{a, b, c\} the complement of the same set is much bigger: it also contains every string with a cc. Always ask "complement with respect to which Σ∗\Sigma^*".

EX 5Typical statements

GATE level

(a) For languages A,BA, B: ∣AB∣=∣A∣∣B∣|AB| = |A||B|: false in general (equality only if every product is different). (b) ∅∗=∅\emptyset^* = \emptyset: false, it is {ε}\{\varepsilon\}. (c) The set of all languages over {0,1}\{0, 1\} is countable: false, it is uncountable. (d) The set of all strings over a finite alphabet is countable: true. (e) If LL is finite then L∗L^* is finite: false unless L⊆{ε}L \subseteq \{\varepsilon\} (e.g. {a}∗\{a\}^* is infinite). (f) (LR)R=L(L^R)^R = L: true. (g) The number of strings of length at most nn over a 33-letter alphabet: (3n+1−1)/2(3^{n+1} - 1)/2.

Cheat box

Cheat box

  • Alphabet: finite non-empty set. String: finite sequence; ε\varepsilon is the string of length 00; ∣xy∣=∣x∣+∣y∣|xy| = |x| + |y|; concatenation is associative, not commutative, ε\varepsilon is its identity.
  • ∣Σn∣=kn|\Sigma^n| = k^n; strings of length ≤n\le n: (kn+1−1)/(k−1)(k^{n+1} - 1)/(k - 1). Σ∗\Sigma^* is countable; the set of all languages is uncountable.
  • A string of length nn: n+1n + 1 prefixes, n+1n + 1 suffixes, at most n(n+1)/2+1n(n+1)/2 + 1 distinct substrings, at most 2n2^n distinct subsequences.
  • Language == any subset of Σ∗\Sigma^*. ABAB, A∗A^*, A+A^+, A‾\overline{A} (w.r.t. Σ∗\Sigma^*), ARA^R.
  • ∅≠{ε}\emptyset \ne \{\varepsilon\}; L∅=∅L\emptyset = \emptyset; L{ε}=LL\{\varepsilon\} = L; ∅∗={ε}\emptyset^* = \{\varepsilon\}; (L∗)∗=L∗(L^*)^* = L^*; (A∪B)∗≠A∗∪B∗(A \cup B)^* \ne A^* \cup B^*.
  • A decision problem is a language; most languages cannot be described by any machine.

Traps and GATE patterns

Connections

A language is the thing a DFA accepts, a grammar generates and a regular expression denotes. The counting of strings reappears in the pumping arguments and in the proof that some problems are undecidable.

PYQs

3 previous year questions from your book. I re-solved each one myself (3 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2025 · Set 1 · NAT · 1 markQ5 · page 197 of the book
Let S be the set of all ternary strings defined over the
alphabet {a, b, c}. Consider all strings in S that contain at
least one occurrence of two consecutive symbols, that is,
"aa", "bb" or "cc". The number of such strings of length 5
that are possible is _______. (Answer in integer)
GATE 2018 · NATQ28 · page 201 of the book
Given a language L, define Li as follows :
L0 = {ε}
Li = Li-1 . L for all i > 0 The order of a language L is
defined as the smallest k such that Lk = Lk+1 . Consider the
language L1 (over alphabet 0) accepted by the following
automation.

The order of L1 is_______.
GATE 2012 · MCQ · 1 markQ52 · page 204 of the book
Given the language L ={ab,aa,baa}, which of the
following strings are in L*?
1)abaabaaabaa
2)aaaabaaaa
3)baaaaabaaaab
4)baaaaabaa
  • (a)1, 2 and 3
  • (b)2, 3 and 4
  • (c)1, 2 and 4
  • (d)1, 3 and 4

Self-check

Question 1

How many strings of length at most 33 are there over {0,1}\{0, 1\}?

Show answer

1+2+4+8=15=24−11 + 2 + 4 + 8 = 15 = 2^4 - 1.

Question 2

What is {ε}∗\{\varepsilon\}^* and what is ∅∗\emptyset^*?

Show answer

Both are {ε}\{\varepsilon\}.

Question 3

How many distinct substrings (including ε\varepsilon) does aaaaaaaa have?

Show answer

55: ε,a,aa,aaa,aaaa\varepsilon, a, aa, aaa, aaaa.

Question 4

Why can some languages not be recognised by any program?

Show answer

There are uncountably many languages but only countably many programs (finite strings over a finite alphabet).

Revision card

One-screen revision

  • Σ\Sigma finite; Σ∗\Sigma^* countable; languages uncountable; ∣Σn∣=kn|\Sigma^n| = k^n.
  • String parts: n+1n + 1 prefixes, ≤n(n+1)/2+1\le n(n+1)/2 + 1 substrings, ≤2n\le 2^n subsequences.
  • Operations: ∪\cup, ∩\cap, complement, ABAB, A∗A^*, A+A^+, reversal.
  • ∅≠{ε}\emptyset \ne \{\varepsilon\}; ∅∗={ε}\emptyset^* = \{\varepsilon\}; L∅=∅L\emptyset = \emptyset.

References

  • ·Sipser, Introduction to the Theory of Computation (3rd edition), Chapter 0 Introduction (mathematical notions and terminology, strings and languages) and Section 1.1
  • ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Section 1.5 The central concepts of automata theory
  • ·Kozen, Automata and Computability, Lecture 1 and Lecture 2

Verification

The counting statements were checked by brute force on 300 random strings (prefixes n + 1, suffixes n + 1, distinct substrings at most n(n+1)/2 + 1, distinct subsequences at most 2^n, and the examples abc and aaa) and on small languages (concatenation size, the number of strings of each length in {a, bb}* is a Fibonacci number, complement and reversal of the parity language). The counts of the table come from running a DFA on every string of each length and, independently, from a dynamic program over the DFA's states, and agree. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never

My notes (saved in this browser)