Theory of Computation · Basics: Alphabets, Strings and Languages
Strings and Counting Strings of Length n
Overview
Strings are sequences of symbols drawn from a finite alphabet, and the symbol is the smallest building block in Theory of Computation.
The key counting result is that an alphabet with symbols yields exactly strings of length , which underlies all later work with languages.
Key concepts
- Symbol: the smallest building block in TOC.
- Alphabet (): a finite set of symbols. It can contain anything — letters (), digits (), or combinations such as . Examples: , , , .
- String: a sequence of symbols taken from an alphabet; every string is formed using only the symbols of that alphabet.
- Hierarchy of concepts: symbols → alphabet (finite set of symbols) → strings (sequences built only from symbols of that alphabet).
How it works
- Fix an alphabet, e.g. .
- A string over it is any sequence of its symbols: and have length 1; has length 2; longer strings such as (over a larger alphabet) follow the same idea.
- To count strings of length , view a string as blank positions to fill.
- Each position can be filled in ways, so multiply the choices across all positions.
Formulas
- — number of strings of length over alphabet ; is the number of symbols in , is the string length (number of positions).
- — the special case , where .
Worked examples
Length 1 over : the strings are , → 2 strings. Formula: . ✔
Length 2 over : enumerate , , , → 4 strings. Formula: . ✔
Length over : positions, 2 choices per position: ( times) .
General alphabet: if , the number of strings of length is . For example, over , , so the length-2 strings number .
Revision summary
- An alphabet is a finite set; a string is a sequence of symbols drawn only from that alphabet.
- Order of concepts: symbol → alphabet → string.
- Memorise: number of strings of length over is ; for it is .
- The length-2 strings over are exactly , , , — 4 strings.
- The count depends only on the number of symbols in the alphabet, not on which symbols they are.
Previous-year exam insights
- Counting strings with a prefix/suffix constraint: fix the constrained positions first (a prefix of length p leaves |Σ|^(n−p) free strings, a suffix of length s leaves |Σ|^(n−s)), and for 'either ... or' use inclusion–exclusion: |A ∪ B| = |A| + |B| − |A ∩ B|, where the intersection fixes both the prefix and the suffix simultaneously.
Asked inISRO 2014UGC NET 2019UGC NET 2012UGC NET 2013 - Number of substrings of a string of length n (counting all contiguous non-empty substrings, including duplicates as distinct positions) is n(n+1)/2; the number of distinct subsequences/prefixes/suffixes is a different count (n prefixes, n suffixes, n+1 including ε).
Asked inUGC NET 2018 - A bit string of length n is a palindrome iff its first ⌈n/2⌉ bits determine the rest, so the number of palindromes is 2^⌈n/2⌉ and the probability a uniformly random length-n bit string is a palindrome is 2^⌈n/2⌉ / = .
Asked inUGC NET 2016 - Number of non-negative integer solutions of x1 + x2 + … + xr = S is C(S + r − 1, r − 1) (stars and bars);
for digit strings, subtract the cases where a digit exceeds 9 by the same formula applied to the shifted variables.
Asked inUGC NET 2015 - For a regular expression, count length-n strings by summing over the choices of each quantified part: a^+ contributes lengths 1..n, a* contributes 0..n, (a|ε) contributes 0 or 1, and multiply the independent factors' counts.
Asked inUGC NET 2016 - A string belongs to a language defined by a length relation (e.g. a^{f(n)}) only if the counts of the two symbol blocks satisfy the stated function exactly; check each candidate by computing f(n) for its block lengths rather than by eyeballing.
Asked inUGC NET 2015