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 of symbols (letters, digits, bits). Examples: , , the ASCII characters.
String (word). A finite sequence of symbols of . The length is the number of symbols. The empty string has length (it is a string, not a symbol, and not the empty set). denotes the number of occurrences of the symbol in .
Concatenation. is followed by ; . It is associative (), has the identity (), and is not commutative (). is concatenated times, .
and . is the set of all strings of length ; is the set of all strings (including ), and . For : . is infinite but countable: its strings can be listed in order of length, and alphabetically within a length (shortlex order).
Parts of a string. is a prefix of if for some ; a suffix if ; a substring (factor) if (a contiguous piece); a subsequence if its symbols appear in 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 . It may be finite or infinite. Examples over : the binary strings that end with ; the strings with equally many s and s; the binary representations of prime numbers.
Operations on languages ():
- union , intersection , difference ;
- complement (it depends on the alphabet);
- concatenation and powers , ;
- Kleene star (any number of strings of , including none), and ;
- reversal .
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 symbols there are exactly strings of length and strings of length at most (for : ). A language is a set of strings, so it can be counted length by length. The table gives, for languages over , 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 n | all strings | ends with 01 | contains 00 | has an even number of 1s | length 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.
The first column is ; "ends with " has strings of length (fix the last two symbols); "has an even number of s" has strings of each length (half of all strings); "length divisible by 3" has strings when is a multiple of and none otherwise.
A string with symbols has:
- exactly prefixes and suffixes (counting and the string itself);
- at most distinct substrings (one for each pair of end positions, plus ), with equality when all symbols are different; the string has , the string only (, , , );
- at most distinct subsequences (one for each subset of positions), again with equality when all symbols are different ( has ).
Languages are uncountably many. is countable, but the set of all subsets of (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 and . Then : three strings, not four, because and coincide. In general . For the star contains , , , , , , , and so on: the strings that can be cut into pieces and ; the number of such strings of length is (a Fibonacci number: the string either ends with or with ).
The empty language and the empty string are different and exam questions use the difference:
| Expression | Value |
|---|---|
| the empty set: no strings at all | |
| the language with exactly one string, the empty one | |
| (nothing to concatenate) | |
| (zero concatenations) | |
| always contains ; contains only if does | |
Also: is not (the first contains for , , the second does not); (De Morgan); (the order reverses).
Worked examples
EX 1Count the strings
EasyOver : strings of length exactly : . Of length at most : . The number of strings of length that start with and end with : the first and last symbols are fixed, three are free: . Strings of length with exactly two s: .
EX 2Substrings and subsequences
Easy: prefixes (); suffixes (); distinct substrings: (, which is less than because letters repeat). The string is a subsequence of (positions and ) but not a substring.
EX 3Evaluate language expressions
Medium, . . (since adds nothing). . . but .
EX 4The complement depends on the alphabet
MediumLet . Then over is the set of strings with no two consecutive s. Over the larger alphabet the complement of the same set is much bigger: it also contains every string with a . Always ask "complement with respect to which ".
EX 5Typical statements
GATE level(a) For languages : : false in general (equality only if every product is different). (b) : false, it is . (c) The set of all languages over is countable: false, it is uncountable. (d) The set of all strings over a finite alphabet is countable: true. (e) If is finite then is finite: false unless (e.g. is infinite). (f) : true. (g) The number of strings of length at most over a -letter alphabet: .
Cheat box
Cheat box
- Alphabet: finite non-empty set. String: finite sequence; is the string of length ; ; concatenation is associative, not commutative, is its identity.
- ; strings of length : . is countable; the set of all languages is uncountable.
- A string of length : prefixes, suffixes, at most distinct substrings, at most distinct subsequences.
- Language any subset of . , , , (w.r.t. ), .
- ; ; ; ; ; .
- A decision problem is a language; most languages cannot be described by any machine.
Traps and GATE patterns
Connections
Builds on
No prerequisites.
Across subjects
No cross-subject links yet.
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
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)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_______.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 are there over ?
Show answerHide
.
Question 2
What is and what is ?
Show answerHide
Both are .
Question 3
How many distinct substrings (including ) does have?
Show answerHide
: .
Question 4
Why can some languages not be recognised by any program?
Show answerHide
There are uncountably many languages but only countably many programs (finite strings over a finite alphabet).
Revision card
One-screen revision
- finite; countable; languages uncountable; .
- String parts: prefixes, substrings, subsequences.
- Operations: , , complement, , , , reversal.
- ; ; .
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)