CS Atlas GATE 2027
TOC course
0 / 91 complete

TOC/Phase 0 · Foundations

Lesson 0.3.1 · Item 8 of 91

Alphabets and strings

Read at your own pace. Try the questions before opening the answers.

On this page

Recall warm-up

  1. In a directed graph, when are there infinitely many paths between two nodes?
    Stuck? Peek at 0.2.5 Directed graphs
    • Directed graph (V, E ⊆ V×V); self-loop; in/out-degree; labelled edges spell words along paths.
    • Path length = #edges. Cycle = path returning to start. Self-loop = cycle of length 1.
    • Reachable set: grow from u until nothing new (BFS). Shortest path in n-node graph ≤ n−1 edges.
    • Infinitely many u→v paths ⇔ some cycle lies on a u→v route.

    Read lesson 0.2.5

  2. A 6-mode machine reads 6 symbols. Must some mode repeat?
    Stuck? Peek at 0.2.4 Pigeonhole principle
    • n + 1 objects in n boxes ⇒ some box has ≥ 2.
    • N objects in k boxes ⇒ some box has ≥ ⌈N/k⌉. To force m in a box: (m−1)k + 1 objects.
    • Machine with n modes, input length ≥ n ⇒ some mode repeats ⇒ a loop in the input.

    Read lesson 0.2.4

  3. How many subsets does a 4-element set have?
    Stuck? Peek at 0.2.1 Sets
    • x ∈ A (element) vs X ⊆ A (subset). ∅ ⊆ every set. A = B ⇔ A ⊆ B and B ⊆ A.
    • A ∪ B, A ∩ B, A − B = A ∩ B̅, A̅ = U − A. De Morgan: (A∪B)̅ = A̅∩B̅, (A∩B)̅ = A̅∪B̅.
    • |A ∪ B| = |A| + |B| − |A ∩ B|. |A × B| = |A|·|B|. |P(A)| = 2^|A|.
    • |∅| = 0, |{∅}| = 1, P(∅) = {∅}.

    Read lesson 0.2.1

The Hook

A machine reads a finite sequence of symbols. A barcode is a sequence over {thick, thin}. A binary number is a sequence over {0, 1}. Your code is a sequence over the keyboard’s characters. To talk precisely about “inputs”, we name the symbols, the sequences, and the basic things you do with them: measure, join, reverse, cut.

Predict First

The word gate has 4 letters. How many different prefixes does it have (beginnings, including the empty beginning and the whole word)?

Core Concept

Prediction check

Answer: 5: (empty), g, ga, gat, gate. A string of length n always has n + 1 prefixes.

gateggagatgate
Prefixes of gate: ε (the empty beginning), g, ga, gat, gate — 5 = n + 1.

Alphabet and string

Alphabet Σ (Greek capital sigma): a finite, non-empty set of symbols. Examples: Σ = {0,1}; Σ = {a,b}; Σ = {a,…,z}.
String (word) over Σ: a finite sequence of symbols from Σ. Over {a,b}: a, abba, bbb.
Σ = {0,1}
binary alphabet
Σ = {a,b}
two letters
abba
a string over {a,b}
abab… forever
not a string: strings are finite
An alphabet is a finite set of symbols; a string is a finite sequence of them.

Length and the empty string

The length |w| is the number of symbols: |abba| = 4. The empty string ε (epsilon) has no symbols: |ε| = 0. It is a real string — the input “nothing at all”.

|w|ₐ counts occurrences of symbol a: |abba|ₐ = 2, |abba|_b = 2.

abba|abba| = 4aa
Length counts symbols; |abba|ₐ = 2. The empty string ε has length 0.

Concatenation

Joining strings: if x = ab and y = ba, then xy = abba and yx = baab. |xy| = |x| + |y|.

  • Order matters: xy ≠ yx in general.
  • Grouping doesn’t: (xy)z = x(yz).
  • ε is the identity: εw = wε = w.

Powers: w⁰ = ε, w¹ = w, wⁿ⁺¹ = wⁿw. So (ab)³ = ababab, a³ = aaa. Notation aⁿ means “a repeated n times”, and aⁿbⁿ means n a’s followed by n b’s (like aaabbb for n = 3).

steps illustration
Concatenation: gluing strings end to end.

Reversal

wᴿ is w written backwards: (abb)ᴿ = bba, εᴿ = ε. Rule: (xy)ᴿ = yᴿxᴿ (reverse, and swap the order — like taking off socks and shoes). A palindrome is a string with w = wᴿ: abba, aba, a, ε.

dcbayᴿ = dcxᴿ = ba
x = ab, y = cd: (xy)ᴿ = (abcd)ᴿ = dcba = yᴿ xᴿ. Reverse and swap, like taking off shoes then socks.

Prefix, suffix, substring

  • Prefix of w: any x with w = xy for some y. (A beginning.)
  • Suffix: any y with w = xy. (An ending.)
  • Substring: any v with w = xvy. (A contiguous middle piece.)
  • “Proper” prefix/suffix: not the whole string w. ε is a prefix, suffix and substring of every string.

Counts for |w| = n: prefixes n + 1; suffixes n + 1; proper prefixes n. Substrings: at most n(n+1)/2 non-empty ones plus ε — exactly that when all symbols are distinct; fewer when pieces repeat.

A subsequence (not the same thing!) keeps the order but may skip symbols: ac is a subsequence of abc but not a substring.

abcaprefix absubstring bcsuffix ca
Prefix = a beginning, suffix = an ending, substring = any contiguous middle. The tinted cells a…c form “ac”: a subsequence (skips b), not a substring.

Putting it together

  • A string is a finite sequence over a finite alphabet; ε has length 0 and is the identity for gluing.
  • Concatenation adds lengths and is not commutative; (xy)ᴿ = yᴿxᴿ.
  • Length n: n+1 prefixes, n+1 suffixes, at most n(n+1)/2 + 1 distinct substrings.

Visual

w = a b c a        |w| = 4

prefixes:  ε  a  ab  abc  abca                 (n+1 = 5)
suffixes:  ε  a  ca  bca  abca                 (n+1 = 5)

substrings by length (all positions):
 len 1:  a  b  c  a        → distinct: a, b, c          (3)
 len 2:  ab bc ca          → 3
 len 3:  abc bca           → 2
 len 4:  abca              → 1
 + ε                        → total distinct = 3+3+2+1+1 = 10
 (formula n(n+1)/2 + 1 = 11 would hold if no piece repeated; "a" repeats once)

reversal: (abca)ᴿ = acba        (xy)ᴿ = yᴿ xᴿ

Worked Examples

Easy

Let x = 01, y = 110. Find xy, yx, |xy|, (xy)ᴿ.

Show the full solution

xy = 01110, yx = 11001, |xy| = 5, (xy)ᴿ = 01110ᴿ = 01110 — it happens to be a palindrome. Check with the rule: yᴿxᴿ = 011·10 = 01110 ✓.

Medium

How many distinct substrings (including ε) does abcd have?

Show the full solution

All symbols distinct, n = 4: n(n+1)/2 + 1 = 10 + 1 = 11.

GATE-level

How many distinct substrings (including ε) does aaaa have? And abab?

Show the full solution

aaaa: the substrings are just ε, a, aa, aaa, aaaa → 5 (one per length). abab: length 1: a, b (2); length 2: ab, ba (2); length 3: aba, bab (2); length 4: abab (1); plus ε → 8. Lesson: the formula is only an upper bound; count by length when symbols repeat.

GATE Traps

  • ε is a string of length 0. It is not a symbol of Σ, and it is not “nothing at all” in the set sense (that will be ∅, next lesson).
  • Substring (contiguous) vs subsequence (may skip). GATE wordings rely on this.
  • (xy)ᴿ = yᴿxᴿ, not xᴿyᴿ.
  • Distinct-substring counts drop when symbols repeat; don’t blindly apply n(n+1)/2 + 1.

Memory Hook

ε is the silent train carriage: hook it anywhere on a train (string) and the train looks exactly the same.

Connect the Dots

We can now handle single strings. Machines, though, accept whole collections of strings. Next lesson combines strings with sets: the set of all strings, and the idea of a language.

Self-Test

  1. NAT

    How many prefixes (including ε and the string itself) does a string of length 7 have?

    Write your answer before opening the key.

  2. MCQ

    Which is always true for strings x, y?

    1. xy = yx
    2. (xy)ᴿ = xᴿyᴿ
    3. (xy)ᴿ = yᴿxᴿ
    4. |xy| = |x|·|y|
  3. NAT

    Number of distinct non-empty substrings of banana?

    Write your answer before opening the key.

  4. MSQ

    Let w = abc. Which are substrings of w?

    1. ε
    2. ac
    3. bc
    4. abc
  5. MCQ

    What is |(ab)³ε|?

    1. 3
    2. 6
    3. 7
    4. 9

Rule Card

  • Σ finite non-empty; string = finite sequence; |ε| = 0; εw = wε = w.
  • |xy| = |x| + |y|; wⁿ repeats w; w⁰ = ε; (xy)ᴿ = yᴿxᴿ.
  • Length n: n+1 prefixes, n+1 suffixes; ≤ n(n+1)/2 + 1 distinct substrings (equality iff all pieces distinct, e.g. all symbols distinct).
  • Substring = contiguous; subsequence = may skip.

Concept Ledger

  • alphabet Σ
  • string (word)
  • length |w|, |w|ₐ
  • empty string ε
  • concatenation xy
  • string power wⁿ, aⁿbⁿ notation
  • reversal wᴿ
  • palindrome
  • prefix, suffix, substring, proper
  • subsequence

Answer Vault

Reveal answers when you’re ready

Warm-up answers

  1. When a cycle lies on some route between them.
  2. Yes: 7 moments > 6 modes.
  3. 16.

Self-test answers

  1. 8 — n + 1.
  2. C — Reverse and swap.
    • A: ab·b = abb ≠ bab.
    • B: Order must swap.
    • D: Lengths add, not multiply.
  3. 15 — Len1: b,a,n (3). Len2: ba,an,na (3). Len3: ban,ana,nan (3). Len4: bana,anan,nana (3). Len5: banan,anana (2). Len6: 1. Total 15.
  4. ACD — Substrings are contiguous.
    • B: ac skips b: a subsequence, not a substring.
  5. B — (ab)³ = ababab has length 6; ε adds 0.
    • A: That counts copies, not symbols.
    • C: ε has length 0.
    • D: Not 3 × 3.

Flashcards

  1. |ε| = ?
    Reveal answer0
  2. (xy)ᴿ = ?
    Reveal answeryᴿ xᴿ
  3. Number of prefixes of a length-n string?
    Reveal answern + 1
  4. Max distinct substrings (incl. ε) of a length-n string?
    Reveal answern(n+1)/2 + 1 (when no piece repeats)
  5. Substring vs subsequence?
    Reveal answerSubstring is contiguous; a subsequence may skip symbols.

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.