Z-Function & Manacher's Algorithm

Data Structures & Algorithms · Advanced Strings · Master · 30 min

Reuse what you already matched: the Z-function finds pattern matches and repeats in O(n), and Manacher's algorithm finds the longest palindromic substring in O(n).

This lesson is part of TuringPath Pro. See plans.

What you will learn

  • The shared trick: a box you already verified
  • Z-function: how far does the start of the string repeat here?
  • Manacher: one radius per centre
  • Why the cap, and why it is O(n)
  • Which string tool when?

More in Advanced Strings

Previous: Digit DP: Counting Numbers Up to N · Next: Rolling Hash & Suffix Arrays