Manacher’s Algorithm is an algorithm that computes for a string of length in time and space, where

Algorithm 0

Applying the definition to find yields an algorithm that solves the problem in time and space.

std::vector<int> manacher(int n, const std::string &s) {
	std::vector d(n, 0);
	for (int i = 0; i < n; i++) {
		while (i - d[i] - 1 >= 0 && i + d[i] + 1 < n && s[i - d[i] - 1] == s[i + d[i] + 1]) {
			d[i]++;
		}
	}
	return d;
}

Algorithm 1

Lemma

Based on Algorithm 0, maintaining and applying the lemma to get an lower bound for yield an algorithm that solves the problem in time and space.

std::vector<int> manacher(int n, const std::string &s) {
	std::vector<int> d(n);
	for (int i = 0, l = 0, r = 0; i < n; i++) {
		d[i] = i < r ? std::min(d[l + r - i - 1], r - i - 1) : 0;
		while (i - d[i] - 1 >= 0 && i + d[i] + 1 < n && s[i - d[i] - 1] == s[i + d[i] + 1]) {
			d[i]++;
		}
		if (i + d[i] + 1 > r) {
			l = i - d[i], r = i + d[i] + 1;
		}
	}
	return d;
}