Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 03 · DSA · Worked, and deeper · about 18 min to read

Play it as a journeyTutor comes with a plan

String Algorithms (finding the needle)

Finding the Needle
Finding the Needle · Open board

Every Ctrl-F, grep, DNA search and spam filter is hunting for one short string inside a much longer one, and the trick that makes that hunt fast is never re-examining a character you already learned something about.

Before you start. You need to be comfortable with an index, a number naming one slot in a list, counting up from the start, and a loop that walks one array one step at a time. The words hash and trie return from Hashing and Trees (BST, balancing, heaps, tries). A one-line reminder rides along the first time each returns.

Lesson 22 of 96 in DSA

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • Ctrl-F’s dirty secret: starting over on every miss
  • 1970: the same fix, found twice within weeks
  • Why your place in the haystack never has to go back
  • What if you never compared a single character?
  • One pass, every needle at once: Aho-Corasick

Terms it explains: KMP, rolling hash, Rabin-Karp, trie, Aho-Corasick, `O(n+m)`, expected, failure link, Boyer-Moore.

Review DSA