ArticleslgStudy

science

Longest palindromic substring

Longest palindromic substring is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Longest palindromic substring rather than just read about it. In short: In computer science, the longest palindromic substring or longest symmetric factor problem is the problem of finding a maximum-length contiguous substring of a given string that is also a palindrome. For example, the longest palindromic substring of "bananas" is "anana".

Key takeaways

  • Longest palindromic substring belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Longest palindromic substring to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Longest palindromic substring from memory before moving on to harder problems.

Reference excerpt

In computer science, the longest palindromic substring or longest symmetric factor problem is the problem of finding a maximum-length contiguous substring of a given string that is also a palindrome. For example, the longest palindromic substring of "bananas" is "anana". The longest palindromic substring is not guaranteed to be unique; for example, in the string "abracadabra", there is no palindromic substring with length greater than three, but there are two palindromic substrings with length three, namely, "aca" and "ada". In some applications it may be necessary to return all maximal palindromic substrings (that is, all substrings that are themselves palindromes and cannot be extended to larger palindromic substrings) rather than returning only one substring or returning the maximum length of a palindromic substring. Manacher (1975) invented an O ( n ) {\displaystyle O(n)} -time algorithm for listing all the palindromes that appear at the start of a given string of length n {\displaystyle n} . However, as observed e.g., by Apostolico, Breslauer & Galil (1995), the same algorithm can also be used to find all maximal palindromic substrings anywhere within the input string, again in O ( n ) {\displaystyle O(n)} time. Therefore, it provides an O ( n ) {\displaystyle O(n)} -time solution to the longest palindromic substring problem. Alternative O ( n ) {\displaystyle O(n)} -time solutions were provided by Jeuring (1994), and by Gusfield (1997), who described a solution based on suffix trees. A faster algorithm can be achieved in the word RAM model of computation if the size σ {\displaystyle \sigma } of the input alphabet is in 2 o ( log ⁡ n ) {\displaystyle 2^{o(\log n)}} . In particular, this algorithm runs in O ( n log ⁡ σ / log ⁡ n ) {\displaystyle O(n\log \sigma /\log n)} time using O ( n log ⁡ σ / log ⁡ n ) {\displaystyle O(n\log \sigma /\log n)} space. Efficient parallel algorithms are also known for the problem. The longest palindromic substring problem should not be confused with the different problem of finding the longest palindromic subsequence.

Slower algorithm This algorithm is slower than Manacher's algorithm, but is a good stepping stone for understanding Manacher's algorithm. It looks at each character as the center of a palindrome and loops to determine the largest palindrome with that center. The loop at the center of the function only works for palindromes where the length is an odd number. The function works for even-length palindromes by modifying the input string. The character '|' is inserted between every character in the inputs string, and at both ends. So the input "book" becomes "|b|o|o|k|". The even-length palindrome "oo" in "book" becomes the odd-length palindrome "|o|o|".

The runtime of this algorithm is O ( n 2 ) {\displaystyle O(n^{2})} . The outer loop runs n {\displaystyle n} times and the inner loop can run up to n / 2 {\displaystyle n/2} times.

Manacher's algorithm Below is the pseudocode for Manacher's algorithm. The algorithm is faster than the previous algorithm because it exploits when a palindrome happens inside another palindrome. For example, consider the input string "abacaba". By the time it gets to the "c", Manacher's algorithm will have identified the length of every palindrome centered on the letters before the "c". At the "c", it runs a loop to identify the largest palindrome centered on the "c": "abacaba". With that knowledge, everything after the "c" looks like the reflection of everything before the "c". The "a" after the "c" has the same longest palindrome as the "a" before the "c". Similarly, the "b" after the "c" has a longest palindrome that is at least the length of the longest palindrome centered on the "b" before the "c". There are some special cases to consider, but that trick speeds up the computation dramatically.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Longest palindromic substring

Start with the simplest possible case. Write down what Longest palindromic substring claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Longest palindromic substring before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Longest palindromic substring ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Longest palindromic substring

In research
Longest palindromic substring appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Longest palindromic substring in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Longest palindromic substring is common in secondary-school and first-year university syllabi. It links to neighbouring topics Palindromes, Problems on strings, so understanding it makes those chapters shorter.
In everyday life
Look for Longest palindromic substring outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Longest palindromic substring” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Longest palindromic substring in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Longest palindromic substring means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Longest palindromic substring out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Longest palindromic substring in simple terms?

In computer science, the longest palindromic substring or longest symmetric factor problem is the problem of finding a maximum-length contiguous substring of a given string that is also a palindrome. For example, the longest palindromic substring of "bananas" is "anana".

Why does Longest palindromic substring matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Longest palindromic substring?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Longest palindromic substring.

Tags

  • Palindromes
  • Problems on strings

Keep exploring