3.8.1 · D1String Algorithms

Foundations — Naive pattern matching — O(nm)

1,752 words8 min readBack to topic

This page builds every piece of notation the parent Naive pattern matching note leans on, starting from "what is a string" and ending at "why ". If a symbol appears on the parent page, it is defined here first, with a picture.


1. What is a string, and what is an index?

A character is one single symbol — one bead. A string is the whole necklace.

To talk about one specific bead, we number the positions. In almost all of computing we start counting at 0, not 1. The number of a position is called its index.

Figure — Naive pattern matching — O(nm)

We write to mean "the character of string sitting at index ." The square brackets are read aloud as "at index". So is the first character, the second, and so on.


2. Length, and the symbols and

The parent note gives two strings special names and two lengths special letters:

Length is ::: n for the text, m for the pattern


3. Slice notation:

The parent writes . This is slice (or substring) notation.

Figure — Naive pattern matching — O(nm)

Why does the parent write ? Because it wants a chunk of the text that is exactly characters long, starting at index . Count them: from to inclusive is characters — the same length as the pattern, so the two can be compared bead-for-bead.

A slice's length is ::: (b - a + 1) characters for T[a..b]


4. The shift and why it stops at

The parent claims there are exactly candidate shifts, from to . Let us see why the last legal shift is , not something else.

Figure — Naive pattern matching — O(nm)

Counting the whole-number shifts gives of them. This is the number of times the outer loop runs.

Recall Quick check of the count

Text length , pattern length . Last shift . Shifts are , which is values . ✔


5. What "" means between two slices (a valid shift)

The parent's definition of a match is

So "" here is not one comparison — it is a promise about all positions at once. To test that promise, we must walk through the positions one at a time. That walk is exactly the inner loop.

A shift where this equality holds is called a valid shift — the thing the algorithm reports.


6. The offset — the inner counter

The pseudocode introduces : the position within the pattern we are currently checking.

Figure — Naive pattern matching — O(nm)

The condition j == m is the signal "the inner loop ran to completion". Since counts up from , reaching the value means it checked indices through — all of them.


7. The symbols behind

The parent's time bound uses the letter and the product . You do not need the full theory (see Big-O Notation), only these plain meanings:

Why does appear? Multiply the two loop counts:


Prerequisite map

Character = one symbol

String = ordered characters

Index = position from 0

Length n and m

Slice T a to b

Shift s from 0 to n-m

Slice equality = valid shift

Offset j inside pattern

Outer loop count n-m+1

Inner loop count up to m

Time O nm

Naive pattern matching


Equipment checklist

If you can answer each of these, you are ready for the parent page.

What does mean, and what is the smallest possible ?
The character of string at index ; the smallest index is (the first character).
If , what is the largest valid index?
— length counts beads, the top index is one less because counting starts at .
What do and stand for?
the text length, the pattern length, with .
How many characters are in the slice ?
characters (both ends included).
Why is the pattern's window written ?
So the window is exactly characters long — same length as — starting at shift .
What is a shift and why must ?
The start index where the pattern is laid down; beyond its tail at runs off the end of the text.
How many candidate shifts exist?
(indices through ).
What must be true for a shift to be valid?
Every pair matches: for all , i.e. the slices are equal.
What does the inner counter track, and what does signal?
How far into the pattern we have walked; means all characters matched → a valid shift.
In one line, why is the cost ?
About windows, each up to comparisons → a rectangle of work with area .

Connections

  • Parent: Naive pattern matching — everything here feeds it.
  • Big-O Notation — the meaning of the ceiling.
  • Substring Search Problem — the larger problem these symbols describe.
  • Sliding Window Technique — the "slide the pattern by increasing " picture generalized.