Skip to main content

1.1 Alphabets, Strings, Substring Combinatorics & Language Formalisms

📚Module 01: Foundations & Deterministic Finite Automata (DFA)1.1 Alphabets, Strings & Language Foundations⏱️5 min read
🎯High-Yield For:Semester Exams • GATE CSE • Technical Interviews

💡 Core Intuition:​

🍳 The Everyday Analogy: Construction Bricks to Architecture​

Imagine constructing a vast metropolis. You cannot build skyscrapers out of arbitrary raw dirt; you need standardized, unyielding atoms:

  1. The Clay Mold (Symbol): An individual, atomic entity (like a letter 'a', a digit '1', or a glyph).
  2. The Brick Palette (Alphabet Σ\Sigma): A strictly finite, non-empty catalog of standardized clay molds. English uses a 26-letter alphabet {A,…,Z}\{A, \dots, Z\}; digital logic uses the binary alphabet {0,1}\{0, 1\}.
  3. The Wall (String ww): A linear sequence created by laying bricks end-to-end. The length is simply the physical count of bricks.
  4. The Blueprint Specification (Language LL): A curated set of approved walls that meet engineering standards. A dictionary is a finite list of words, but sentences in human speech are potentially infinite.

💻 Bridging to Computer Science: Natural vs Formal Languages​

Human natural languages (English, Hindi, Mandarin) are deeply ambiguous and context-dependent. A phrase like "The man saw the boy with binoculars" requires human semantic parsing to decide who holds the binoculars.

Digital computing hardware cannot tolerate ambiguity. An instruction decoder or compiler lexer requires a formal language: an unambiguous mathematical specification governed by rigid rules where every symbol sequence is decidably valid or invalid.

Architecture Flow

The Mathematical Hierarchy of Formal Languages

Hierarchical composition: from indivisible atomic symbols to infinite language sets

🔤Element

Symbol

Indivisible atomic abstract entity (e.g., 0, 1, a, b).

→
Grouping
📦Finite Set

Alphabet (Σ)

Non-empty finite collection of valid symbols.

→
Concatenation
📜Sequence

String (w)

Finite-length sequence formed from alphabet symbols.

→
Collection
📚Subset

Language (L)

Subset of Σ* containing strings satisfying a specific property.


🧱 Mathematical Building Blocks: Symbols, Alphabets & Strings​

In the Theory of Computation, computation is modeled as the processing of finite sequences over discrete alphabets.

Natural Languages vs Formal Mathematical Languages

Why computing systems mandate rigid formal language definitions over ambiguous natural languages

Natural Language

Human Linguistic Communication

🗣️
Dominant Architecture / DomainEnglish, Hindi, Mandarin
  • •Inherently ambiguous grammar rules with multiple interpretation paths
  • •Dictionary contains finite words, but semantic meanings drift over time
  • •Requires deep contextual inference and commonsense world models
  • •Context-sensitive syntax that baffles deterministic state machines
"Human languages trade precision for expressive flexibility."
Formal Language

Mathematical Automata Specification

⚙️
Dominant Architecture / DomainTOC, Compilers, Protocols, Digital Logic
  • •Unambiguous mathematical definitions governed by discrete state transitions
  • •Strictly defined over a finite, immutable alphabet Σ
  • •Determined by formal grammar generators or finite machine acceptors
  • •Deterministic membership: every string w either belongs to L or does not
"Formal languages trade poetic nuance for absolute mathematical determinism."

🔤 Symbol Definition​

A symbol is an indivisible, atomic element (a character, token, or letter).

  • Examples: 0,1,a,b,x,y,⋆,♣0, 1, a, b, x, y, \star, \clubsuit.

🔤 Alphabet (Σ)​

An alphabet Σ\Sigma is a finite, non-empty set of symbols.

  • Binary Alphabet: Σ={0,1}\Sigma = \{0, 1\}
  • English Lowercase Alphabet: Σ={a,b,c,…,z}\Sigma = \{a, b, c, \dots, z\}
  • Hexadecimal Alphabet: Σ={0,1,…,9,A,B,C,D,E,F}\Sigma = \{0, 1, \dots, 9, A, B, C, D, E, F\}

Theoretical Invariant: An alphabet can never be empty (Σ≠∅\Sigma \ne \emptyset) and can never be infinite (∣Σ∣<∞|\Sigma| < \infty).

📜 String and Word Foundations (w)​

A string ww is a finite sequence of symbols chosen from an alphabet Σ\Sigma.

  • For Σ={a,b}\Sigma = \{a, b\}, candidate strings include w1=abw_1 = ab, w2=aabbw_2 = aabb, w3=bbaw_3 = bba.
  • The length of a string, denoted ∣w∣|w|, is the total count of symbols in the sequence: If w=00110  ⟹  ∣w∣=5\text{If } w = 00110 \implies |w| = 5

📐 Operations on Strings: Concatenation, Reversal & Powers​

Just as numbers support arithmetic operations, strings support fundamental algebraic operations.

The 4 Core String Operations

The fundamental algebraic operations that construct and transform symbol sequences

🔗

String Concatenation

Sequence Append
  • Appending string y directly to the end of string x: w = xy.
  • Additive Length Invariant: |xy| = |x| + |y|.
  • Non-commutative: xy ≠ yx in general (e.g. ab · ba ≠ ba · ab).
🔄

String Reversal

Order Inversion
  • Writing symbols in opposite sequence: w = a₁a₂...aₙ ⟹ wᴿ = aₙ...a₂a₁.
  • Length Preservation: |wᴿ| = |w|.
  • Reversal Involution: (wᴿ)ᴿ = w and (xy)ᴿ = yᴿxᴿ.
⚡

String Powers (wⁿ)

Iterated Concatenation
  • Concatenating string w with itself n times: wⁿ = w · w · ... · w.
  • Base Case: w⁰ = ε (the empty string).
  • Length Scaling: |wⁿ| = n · |w|.
⭕

Empty String (ε or λ)

Identity Element
  • The unique string containing zero symbols: |ε| = 0.
  • Identity Invariant: w · ε = ε · w = w.
  • Exists over any alphabet: ε ∈ Σ* for all valid alphabets Σ.

🔢 Combinatorial Theorems: Substring, Prefix & Suffix Counting​

A string ww can be decomposed into contiguous segments. In automata design, understanding the combinatorial boundaries of prefixes, suffixes, and substrings is mandatory.

✂️ Prefixes Analysis​

A prefix of ww is any leading segment obtained by truncating zero or more symbols from the right end: x is a prefix of w  ⟺  ∃y such that w=xyx \text{ is a prefix of } w \iff \exists y \text{ such that } w = xy

  • For w="GATE"w = \text{"GATE"}, the prefixes are: Prefixes(w)={ϵ,G,GA,GAT,GATE}\text{Prefixes}(w) = \{\epsilon, \text{G}, \text{GA}, \text{GAT}, \text{GATE}\}
  • Theorem: For any string ww of length nn, the total number of prefixes is: Total Prefixes=n+1(including ϵ)\text{Total Prefixes} = n + 1 \quad (\text{including } \epsilon)
  • A proper prefix excludes the string itself: Proper Prefixes=n\text{Proper Prefixes} = n

🏷️ Suffixes Analysis​

A suffix of ww is any trailing segment obtained by truncating zero or more symbols from the left end: y is a suffix of w  ⟺  ∃x such that w=xyy \text{ is a suffix of } w \iff \exists x \text{ such that } w = xy

  • For w="GATE"w = \text{"GATE"}, the suffixes are: Suffixes(w)={ϵ,E,TE,ATE,GATE}\text{Suffixes}(w) = \{\epsilon, \text{E}, \text{TE}, \text{ATE}, \text{GATE}\}
  • Theorem: For any string ww of length nn, the total number of suffixes is: Total Suffixes=n+1(including ϵ)\text{Total Suffixes} = n + 1 \quad (\text{including } \epsilon)
  • A proper suffix excludes the string itself: Proper Suffixes=n\text{Proper Suffixes} = n

🧩 Substrings Analysis​

A substring is any contiguous block of symbols appearing within ww: s is a substring of w  ⟺  ∃x,y such that w=xsys \text{ is a substring of } w \iff \exists x, y \text{ such that } w = xsy

📐Substring Counting Theorem (Distinct Symbols)theorem

Let ww be a string of length nn composed of nn pairwise distinct symbols.

  1. Substrings of length 11: nn choices.
  2. Substrings of length 22: n−1n - 1 choices.
  3. Substrings of length kk: n−k+1n - k + 1 choices.
  4. Substrings of length nn: 11 choice (ww itself).
  5. Substrings of length 00: 11 choice (ϵ\epsilon).

Summing all contiguous segments: ∑k=1n(n−k+1)+1=n(n+1)2+1\sum_{k=1}^n (n - k + 1) + 1 = \frac{n(n + 1)}{2} + 1

Counting Invariants:

  • Total Substrings (including ϵ\epsilon): n(n+1)2+1\frac{n(n+1)}{2} + 1
  • Total Non-Empty Substrings (excluding ϵ\epsilon): n(n+1)2\frac{n(n+1)}{2}
  • Total Non-Trivial Substrings (excluding ϵ\epsilon and ww): n(n+1)2−1\frac{n(n+1)}{2} - 1

[!WARNING] If a string contains duplicate symbols (e.g., w="aaaa"w = \text{"aaaa"} or w="abab"w = \text{"abab"}), multiple index slices produce identical substrings. The formula n(n+1)2+1\frac{n(n+1)}{2} + 1 represents the strict upper bound, achieved only when all symbols in ww are distinct!


🌐 Formal Languages & Set Operations: Complements & Reversals​

A language LL over an alphabet Σ\Sigma is any subset of strings formed from Σ\Sigma: L⊆Σ∗L \subseteq \Sigma^*

Because languages are formal sets, they inherit both standard set operations and string-specific transformations.

Language Abstraction Stack

Hierarchical construction of formal languages from atomic symbols to infinite language classes

Layer 4

Language Classes & Automata

🏛️

Infinite sets of approved strings accepted by state machines (DFA, NFA, PDA, Turing Machines).

Regular LanguagesContext-Free LanguagesDecidable Languages
↓Accepted by Automata / Generated by Grammars
Layer 3

Formal Languages (L ⊆ Σ*)

🌐

Specific subsets of strings satisfying mathematical invariants (e.g. even parity, palindromes).

L = {aⁿbⁿ | n ≥ 0}L = {w | |w| mod 3 = 0}
↓Aggregated into Language Subsets
Layer 2

Finite Strings (w ∈ Σ*)

🧵

Linear sequences formed by concatenating symbols: w = s₁s₂...sₖ.

w = 110w = abba|w| = length
↓Concatenated from Symbols
Layer 1

Atomic Alphabet (Σ)

🧱

A finite, non-empty set of discrete symbols establishing the fundamental domain of discourse.

Binary Σ = {0, 1}ASCII Character Set

Key Language Operations​

  1. Union (L1∪L2L_1 \cup L_2): L1∪L2={w∣w∈L1∨w∈L2}L_1 \cup L_2 = \{w \mid w \in L_1 \lor w \in L_2\}
  2. Intersection (L1∩L2L_1 \cap L_2): L1∩L2={w∣w∈L1∧w∈L2}L_1 \cap L_2 = \{w \mid w \in L_1 \land w \in L_2\}
  3. Language Complement (LcL^c or Lˉ\bar{L}): The set of all strings over Σ\Sigma that do not belong to LL: Lc=Σ∗−LL^c = \Sigma^* - L
  4. Language Reversal (LRL^R): The set of all reversed strings: LR={wR∣w∈L}L^R = \{w^R \mid w \in L\}
    • Example: If L={ab,aab}  ⟹  LR={ba,baa}L = \{ab, aab\} \implies L^R = \{ba, baa\}.
  5. Language Concatenation (L1L2L_1 L_2): L1L2={xy∣x∈L1,y∈L2}L_1 L_2 = \{xy \mid x \in L_1, y \in L_2\}
    • Example: If L1={ab,aa}L_1 = \{ab, aa\} and L2={b,ba}L_2 = \{b, ba\}: L1L2={abb,abba,aab,aaba}L_1 L_2 = \{abb, abba, aab, aaba\}

♾️ Kleene Closure (Σ*) vs Positive Closure (Σ+)​

The most fundamental language generator over an alphabet is the Kleene Star closure.

Kleene Closure (Σ*) vs Positive Closure (Σ+)

The presence or absence of the zero-length identity string ε defines closure semantics

Kleene Star (Σ*)

Universal Closure with ε

⭐
Dominant Architecture / DomainAll Strings of Length ≥ 0
  • •Infinite union: Σ* = Σ⁰ ∪ Σ¹ ∪ Σ² ∪ Σ³ ∪ ... ∪ Σ^∞
  • •Always contains the empty string ε (since Σ⁰ = {ε})
  • •Universal set: Every language over Σ is a subset of Σ* (L ⊆ Σ*)
  • •Cardinality: |Σ*| is countably infinite (ℵ₀) for any non-empty Σ
"Σ* represents the universe of all possible finite strings over an alphabet."
Positive Closure (Σ+)

Non-Empty String Closure

➕
Dominant Architecture / DomainAll Strings of Length ≥ 1
  • •Infinite union excluding zero length: Σ⁺ = Σ¹ ∪ Σ² ∪ Σ³ ∪ ... ∪ Σ^∞
  • •Does NOT contain ε unless ε is explicitly in the base alphabet
  • •Fundamental Relation: Σ* = Σ⁺ ∪ {ε}
  • •Equivalence: Σ⁺ = Σ · Σ* = Σ* · Σ
"Σ⁺ represents all non-empty communications formed from the alphabet."

Mathematical Powers of an Alphabet (Σ^k)​

Let Σ={0,1}\Sigma = \{0, 1\}:

  • Σ0={ϵ}\Sigma^0 = \{\epsilon\} (all strings of length 00)   ⟹  ∣Σ0∣=1\implies |\Sigma^0| = 1
  • Σ1={0,1}\Sigma^1 = \{0, 1\} (all strings of length 11)   ⟹  ∣Σ1∣=2\implies |\Sigma^1| = 2
  • Σ2={00,01,10,11}\Sigma^2 = \{00, 01, 10, 11\} (all strings of length 22)   ⟹  ∣Σ2∣=4\implies |\Sigma^2| = 4
  • General Invariant: For an alphabet of size ∣Σ∣=m|\Sigma| = m, the number of strings of length kk is: ∣Σk∣=mk|\Sigma^k| = m^k

🏭 Real-World Production Context:​

  1. Compiler Lexical Tokenizers (Lex / Flex): Compilers represent source code as a string ww over the ASCII or UTF-8 alphabet Σ\Sigma. Lexical analyzers scan prefixes of the remaining input string, identifying the longest prefix that matches a token definition (identifiers, keywords, numeric literals).
  2. Network Protocol Framing: Ethernet and TCP/IP packets are strings of bits over Σ={0,1}\Sigma = \{0, 1\}. Frame delimitation uses specialized prefix/suffix flags (e.g., HDLC 01111110), and bit-stuffing guarantees that the flag substring never appears in the payload.
  3. Genomic Sequence Analysis (Bioinformatics): DNA sequencing treats chromosomes as giant strings over the 4-symbol alphabet Σ={A,C,G,T}\Sigma = \{A, C, G, T\}. Suffix trees and substring indices (FM-index / Burrows-Wheeler Transform) allow fast O(m)O(m) substring search queries across 3 billion base pairs.

📐 Numerical Applications & Worked Examples​

Problem 1: Substring Counting on Strings with Distinct Symbols​

Problem Statement:
Consider the string w="CIPHER"w = \text{"CIPHER"} defined over the English alphabet.

  1. Calculate the total number of substrings.
  2. Calculate the total number of non-empty substrings.
  3. Calculate the total number of proper prefixes and proper suffixes.

Step-by-Step Solution:

  1. Verify Distinctness:
    The string w="CIPHER"w = \text{"CIPHER"} has length n=6n = 6. All 6 characters (C, I, P, H, E, R\text{C, I, P, H, E, R}) are distinct.

  2. Total Substrings (Including ϵ\epsilon):
    Using the combinatorial formula: Total Substrings=n(n+1)2+1=6(7)2+1=21+1=22\text{Total Substrings} = \frac{n(n + 1)}{2} + 1 = \frac{6(7)}{2} + 1 = 21 + 1 = 22

  3. Non-Empty Substrings:
    Excluding the empty string ϵ\epsilon: Non-Empty Substrings=n(n+1)2=6×72=21\text{Non-Empty Substrings} = \frac{n(n + 1)}{2} = \frac{6 \times 7}{2} = 21

  4. Proper Prefixes and Suffixes:

    • Number of prefixes: n+1=6+1=7n + 1 = 6 + 1 = 7 ({ϵ,C,CI,CIP,CIPH,CIPHE,CIPHER}\{\epsilon, \text{C}, \text{CI}, \text{CIP}, \text{CIPH}, \text{CIPHE}, \text{CIPHER}\}).
    • Number of proper prefixes (excluding ww itself): n=6n = 6.
    • Number of proper suffixes (excluding ww itself): n=6n = 6.

Problem 2: Substring Counting with Repeated Characters​

Problem Statement:
Consider the string w="banana"w = \text{"banana"} over Σ={a,b,n}\Sigma = \{a, b, n\}. Determine the exact number of distinct substrings and contrast it against the distinct-symbol upper bound.

Step-by-Step Solution:

  1. Calculate Theoretical Upper Bound:
    For n=∣w∣=6n = |w| = 6: Upper Bound=6(7)2+1=22 substrings\text{Upper Bound} = \frac{6(7)}{2} + 1 = 22 \text{ substrings}

  2. Enumerate Distinct Substrings by Length:

    • Length 0 (k=0k=0): {ϵ}\{\epsilon\} (1 substring)
    • Length 1 (k=1k=1): {b,a,n}\{b, a, n\} (3 substrings; duplicate aa's and nn's collapse)
    • Length 2 (k=2k=2): {ba,an,na}\{ba, an, na\} (3 substrings; "an" appears twice, "na" appears twice)
    • Length 3 (k=3k=3): {ban,ana,nan}\{ban, ana, nan\} (3 substrings; "ana" appears twice)
    • Length 4 (k=4k=4): {bana,anan,nana}\{bana, anan, nana\} (3 substrings)
    • Length 5 (k=5k=5): {banan,anana}\{banan, anana\} (2 substrings)
    • Length 6 (k=6k=6): {banana}\{banana\} (1 substring)
  3. Total Actual Distinct Substrings:
    Total=1+3+3+3+3+2+1=16 distinct substrings\text{Total} = 1 + 3 + 3 + 3 + 3 + 2 + 1 = 16 \text{ distinct substrings}

  4. Conclusion:
    Due to internal periodicities and repeating characters, the actual count (1616) is strictly less than the theoretical maximum (2222).


Problem 3: Alphabet Powers & Language Cardinality​

Problem Statement:
Let Σ={0,1}\Sigma = \{0, 1\}.

  1. Determine the cardinality of the language L=Σ0∪Σ1∪Σ2∪Σ3L = \Sigma^0 \cup \Sigma^1 \cup \Sigma^2 \cup \Sigma^3.
  2. If L1={w∈Σ∗∣∣w∣=2}L_1 = \{w \in \Sigma^* \mid |w| = 2\} and L2={w∈Σ∗∣∣w∣≤1}L_2 = \{w \in \Sigma^* \mid |w| \le 1\}, compute ∣L1L2∣|L_1 L_2|.

Step-by-Step Solution:

  1. Cardinality of LL:
    Each Σk\Sigma^k contains strings of length kk: ∣Σ0∣=20=1({ϵ})|\Sigma^0| = 2^0 = 1 \quad (\{\epsilon\}) ∣Σ1∣=21=2({0,1})|\Sigma^1| = 2^1 = 2 \quad (\{0, 1\}) ∣Σ2∣=22=4({00,01,10,11})|\Sigma^2| = 2^2 = 4 \quad (\{00, 01, 10, 11\}) ∣Σ3∣=23=8({000,001,…,111})|\Sigma^3| = 2^3 = 8 \quad (\{000, 001, \dots, 111\}) Since sets of different length strings are mutually disjoint: ∣L∣=1+2+4+8=15 strings|L| = 1 + 2 + 4 + 8 = 15 \text{ strings}

  2. Language Concatenation L1L2L_1 L_2:

    • L1={00,01,10,11}L_1 = \{00, 01, 10, 11\} (size 44)
    • L2={ϵ,0,1}L_2 = \{\epsilon, 0, 1\} (size 33)
    • Compute pairwise concatenations xyxy where x∈L1,y∈L2x \in L_1, y \in L_2:
      • Concatenating with ϵ\epsilon: {00,01,10,11}\{00, 01, 10, 11\} (4 strings of length 2)
      • Concatenating with 00: {000,010,100,110}\{000, 010, 100, 110\} (4 strings of length 3)
      • Concatenating with 11: {001,011,101,111}\{001, 011, 101, 111\} (4 strings of length 3)
    • Check for collisions: Length 2 strings cannot collide with length 3 strings. Within length 3 strings, suffixes 00 and 11 guarantee no collisions. ∣L1L2∣=4+4+4=12 strings|L_1 L_2| = 4 + 4 + 4 = 12 \text{ strings}

🎯 Exam & Interview Pitfall Check​

Core Conceptual Questions
  • Can an alphabet be infinite?
    No. By definition, an alphabet Σ\Sigma is a finite, non-empty set of symbols. If a set of symbols is infinite, it cannot form an alphabet in standard automata theory.
  • Is ϵ\epsilon an element of every language?
    No. ϵ∈Σ∗\epsilon \in \Sigma^* is true for every alphabet, but a specific language L⊆Σ∗L \subseteq \Sigma^* may or may not include ϵ\epsilon. For example, L={w∈{0,1}∗∣∣w∣≥1}L = \{w \in \{0, 1\}^* \mid |w| \ge 1\} explicitly excludes ϵ\epsilon.
  • What is the difference between ∅\emptyset and {ϵ}\{\epsilon\}?
    ∅\emptyset is the empty language with cardinality ∣∅∣=0|\emptyset| = 0 (contains zero strings). {ϵ}\{\epsilon\} is a non-empty language with cardinality ∣{ϵ}∣=1|\{\epsilon\}| = 1 (contains exactly one string of length zero).
Common Interview Traps
  • Commutativity Trap in String Concatenation: Many candidates incorrectly assume xy=yxxy = yx. String concatenation is associative ((xy)z=x(yz)(xy)z = x(yz)), but strictly non-commutative (01≠1001 \ne 10).
  • The "Distinct Symbols" Condition in Substring Counting: Never blindly write n(n+1)2+1\frac{n(n+1)}{2} + 1 without checking whether the string contains duplicate characters. For w="aaaa"w = \text{"aaaa"}, the formula yields 4(5)2+1=11\frac{4(5)}{2}+1 = 11, but the actual count is only 55 ({ϵ,a,aa,aaa,aaaa}\{\epsilon, a, aa, aaa, aaaa\}).
  • Language Power vs String Power: Do not confuse w2w^2 (a single string wwww) with L2L^2 (a set of strings formed by pairwise concatenation {xy∣x,y∈L}\{xy \mid x, y \in L\}).
💬

Discussion & Doubts