1. Gyanastra
  2. Theory of Computation
  3. Basics: Alphabets, Strings and Languages
  4. Strings and Counting Strings of Length n
Free lesson, no account needed 4 min read 7 practice questions after it
Asked in past papers8 times
  • GATE CSE2021
  • ISRO2014
  • UGC NET201920182016201520132012

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 ∣Σ∣|\Sigma| symbols yields exactly ∣Σ∣n|\Sigma|^n strings of length nn, which underlies all later work with languages.

Key concepts

  • Symbol: the smallest building block in TOC.
  • Alphabet (Σ\Sigma): a finite set of symbols. It can contain anything — letters (a,b,ca, b, c), digits (0,1,2,90, 1, 2, 9), or combinations such as {0,1,2,9}\{0, 1, 2, 9\}. Examples: Σ={a,b}\Sigma = \{a, b\}, Σ={A,B,C}\Sigma = \{A, B, C\}, Σ={0,1}\Sigma = \{0, 1\}, Σ={0,1,2,9}\Sigma = \{0, 1, 2, 9\}.
  • 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

  1. Fix an alphabet, e.g. Σ={a,b}\Sigma = \{a, b\}.
  2. A string over it is any sequence of its symbols: aa and bb have length 1; aaaa has length 2; longer strings such as abbcabbc (over a larger alphabet) follow the same idea.
  3. To count strings of length nn, view a string as nn blank positions to fill.
  4. Each position can be filled in ∣Σ∣|\Sigma| ways, so multiply the choices across all nn positions.

Formulas

  • ∣Σ∣n|\Sigma|^n — number of strings of length nn over alphabet Σ\Sigma; ∣Σ∣|\Sigma| is the number of symbols in Σ\Sigma, nn is the string length (number of positions).
  • 2n2^n — the special case Σ={a,b}\Sigma = \{a, b\}, where ∣Σ∣=2|\Sigma| = 2.

Worked examples

Length 1 over Σ={a,b}\Sigma = \{a, b\}: the strings are aa, bb → 2 strings. Formula: 21=22^1 = 2. ✔

Length 2 over Σ={a,b}\Sigma = \{a, b\}: enumerate aaaa, abab, baba, bbbb → 4 strings. Formula: 22=2×2=42^2 = 2 \times 2 = 4. ✔

Length nn over Σ={a,b}\Sigma = \{a, b\}: nn positions, 2 choices per position: 2×2×…2 \times 2 \times \dots (nn times) =2n= 2^n.

General alphabet: if ∣Σ∣=k|\Sigma| = k, the number of strings of length nn is knk^n. For example, over Σ={0,1,2,9}\Sigma = \{0, 1, 2, 9\}, ∣Σ∣=4|\Sigma| = 4, so the length-2 strings number 42=164^2 = 16.

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 nn over Σ\Sigma is ∣Σ∣n|\Sigma|^n; for Σ={a,b}\Sigma = \{a, b\} it is 2n2^n.
  • The length-2 strings over {a,b}\{a, b\} are exactly aaaa, abab, baba, bbbb — 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⌉ / 2n2^{n} = 2−⌊n/2⌋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)}bnb^{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
Pages 1–2 of 2