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 06 · DSA · Clinic: search, trees, graphs and heaps · about 22 min to read

Play it as a journeyTutor comes with a plan

More string tools: Z, Manacher and suffix arrays

Finding the Needle
Finding the Needle · Open board

One short string hunted inside a long one, the picture that every tool in this lesson redraws with a different trick.

Someone gives you a text of 100,000 letters and asks: what is the longest palindrome inside it? A palindrome is a string that reads the same forwards and backwards, like racecar. The simple method takes each possible middle and grows outward while the two sides match. On the worst text that costs 100,000 × 100,001 ÷ 2 = 5,000,050,000 steps. A second person asks: this text will be searched ten thousand times, each time for a new word. A fresh scan every time is also slow.

By the end you will be able to compute a Z-array, find the longest palindrome in about 2 steps per letter with Manacher’s algorithm, build and search a suffix array, and parse 3+2*2 with one stack (LeetCode 227 and 224).

The history (optional)

Glenn Manacher published his palindrome algorithm in the Journal of the ACM, volume 22, issue 3, July 1975, pages 346 to 351. Robert Boyer and J Strother Moore published their skipping search in Communications of the ACM, October 1977. Udi Manber and Gene Myers presented suffix arrays at the SODA conference in 1990. Their abstract says suffix arrays take three to five times less space than the older suffix tree, and three to ten times longer to build.

Before you start. String Algorithms (finding the needle) gave you the idea that powers this lesson: never throw away what a partial match taught you, so your position in the text only moves forward. Linked Lists, Stacks & Queues gave you the stack, a list where you only add and remove at one end. Clinic #11: Binary Search gave you binary search, which finds a value in a sorted list by halving the range, and Arrays & Strings gave you indexes. In this lesson a prefix is the first few letters of a string, and a suffix is the last few letters.

Lesson 47 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

  • The Z-array: how much of the start matches here?
  • The Z-box: reuse the answer you already have
  • Palindrome expansion: grow out from every middle
  • Manacher’s algorithm: a palindrome carries a mirror inside it
  • The suffix array: every ending, in dictionary order
  • The Z-array and a search with it
  • Manacher, and LeetCode 5
  • The suffix array by prefix doubling
  • Boyer-Moore skipping, in its simple form
  • Expression parsing: LeetCode 227 and 224

Terms it explains: palindrome, prefix, suffix, Z-array, Z-box, palindrome expansion, radius, Manacher's algorithm, suffix array, Common mistakes, Prefix doubling, end.

Review DSA