Intuition The one core idea
To find a small word hidden inside a big text, you slide the small word along the text one letter at a time and, at each stopping point, spell it out letter-by-letter to see if it fits. Everything else on the parent page — the symbols, the loops, the O ( nm ) — is just the careful bookkeeping needed to describe that sliding-and-spelling precisely.
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 O ( nm ) ". If a symbol appears on the parent page, it is defined here first, with a picture.
A string is just an ordered row of characters (letters, digits, symbols) sitting side by side, like beads on a wire. "Ordered" means position matters: "AB" is not the same string as "BA".
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 .
Intuition Why start at 0?
The index answers the question "how many beads are before me?" The very first bead has zero beads before it → index 0 . This convention makes the arithmetic of "slide by s " clean: position s literally means "s steps in from the start".
We write T [ i ] to mean "the character of string T sitting at index i ." The square brackets are read aloud as "at index". So T [ 0 ] is the first character, T [ 1 ] the second, and so on.
Worked example Reading off characters
Let T = "ABABC". Then T [ 0 ] = A, T [ 1 ] = B, T [ 2 ] = A, T [ 3 ] = B, T [ 4 ] = C.
The parent note gives two strings special names and two lengths special letters:
Common mistake Length vs. last index
If len ( T ) = n , the last valid index is n − 1 , not n . A string of length 5 uses indices 0 , 1 , 2 , 3 , 4 . Asking for T [ 5 ] runs off the end — an "out of bounds" error. Length counts beads; the highest index is always one less because we started at 0 .
Length is ::: n for the text, m for the pattern
The parent writes T [ s … s + m − 1 ] . This is slice (or substring ) notation.
T [ a … b ] means "the run of characters of T from index a up to and including index b " — a scoop of consecutive beads.
Worked example Slicing a string
With T = "ABABC": T [ 1 … 3 ] is the characters at indices 1 , 2 , 3 = "BAB". It has 3 − 1 + 1 = 3 characters.
Why does the parent write T [ s … s + m − 1 ] ? Because it wants a chunk of the text that is exactly m characters long , starting at index s . Count them: from s to s + m − 1 inclusive is ( s + m − 1 ) − s + 1 = m 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]
A shift s is a candidate starting index — the position in T where we try laying down the left edge of the pattern. Sliding the pattern one step to the right means increasing s by 1 .
The parent claims there are exactly n − m + 1 candidate shifts, from s = 0 to s = n − m . Let us see why the last legal shift is n − m , not something else.
s can go no further than n − m
When you place the pattern at shift s , its rightmost character lands at index s + m − 1 . For that to still be inside the text, we need s + m − 1 ≤ n − 1 . Subtract and rearrange: s ≤ n − m . Push s one more and the pattern's tail pokes off the right end of the text — no character to compare against.
Counting the whole-number shifts 0 , 1 , 2 , … , n − m gives == n − m + 1 == of them. This is the number of times the outer loop runs.
Recall Quick check of the count
Text length n = 10 , pattern length m = 3 . Last shift = n − m = 7 . Shifts are 0 , 1 , … , 7 , which is 8 values = n − m + 1 = 10 − 3 + 1 . ✔
The parent's definition of a match is
T [ s … s + m − 1 ] = P [ 0 … m − 1 ] .
Definition Slice equality
Two slices are equal when they have the same length and every pair of characters in the same position agrees: T [ s ] = P [ 0 ] , T [ s + 1 ] = P [ 1 ] , …, T [ s + m − 1 ] = P [ m − 1 ] .
So "= " here is not one comparison — it is a promise about all m 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 s where this equality holds is called a valid shift — the thing the algorithm reports.
The pseudocode introduces j : the position within the pattern we are currently checking.
Intuition Two rulers, two counters
Think of s as which window of the text we are looking at, and j as how far into the pattern we have walked inside that window. While s is frozen, j climbs 0 , 1 , 2 , … comparing T [ s + j ] against P [ j ] . The combined index s + j is "start of window plus how far in" — it always points at the text character sitting under P [ j ] .
The condition j == m is the signal "the inner loop ran to completion". Since j counts up from 0 , reaching the value m means it checked indices 0 through m − 1 — all of them.
The parent's time bound uses the letter O and the product nm . You do not need the full theory (see Big-O Notation ), only these plain meanings:
Definition Big-O, in one breath
==O ( something ) == i s a ce i l in g o nh o w t h e w or k g r o w s a s t h e in p u t s g r o w — i g n or in g co n s t an t f a c t or s an d o n l y k ee p in g t h e f a s t es t − g r o w in g p a r t . O(nm)m e an s " t h e n u mb er o f co m p a r i so n s g r o w s ∗ a tw or s t ∗ in p r o p or t i o n t o nt im es m$".
Why does n × m appear? Multiply the two loop counts:
String = ordered characters
Slice equality = valid shift
If you can answer each of these, you are ready for the parent page.
What does T [ i ] mean, and what is the smallest possible i ? The character of string T at index i ; the smallest index is 0 (the first character).
If len ( T ) = n , what is the largest valid index? n − 1 — length counts beads, the top index is one less because counting starts at 0 .
What do n and m stand for? n = len ( T ) the text length, m = len ( P ) the pattern length, with m ≤ n .
How many characters are in the slice T [ a … b ] ? b − a + 1 characters (both ends included).
Why is the pattern's window written T [ s … s + m − 1 ] ? So the window is exactly m characters long — same length as P — starting at shift s .
What is a shift s and why must s ≤ n − m ? The start index where the pattern is laid down; beyond n − m its tail at s + m − 1 runs off the end of the text.
How many candidate shifts exist? n − m + 1 (indices 0 through n − m ).
What must be true for a shift s to be valid ? Every pair matches: T [ s + j ] = P [ j ] for all j = 0 … m − 1 , i.e. the slices are equal.
What does the inner counter j track, and what does j = m signal? How far into the pattern we have walked; j = m means all m characters matched → a valid shift.
In one line, why is the cost O ( nm ) ? About n windows, each up to m comparisons → a rectangle of work with area nm .
Parent: Naive pattern matching — everything here feeds it.
Big-O Notation — the meaning of the O ( nm ) ceiling.
Substring Search Problem — the larger problem these symbols describe.
Sliding Window Technique — the "slide the pattern by increasing s " picture generalized.