> For the complete documentation index, see [llms.txt](https://blog.bernardw.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.bernardw.com/problems/longest-palindromic-substring.md).

# Longest Palindromic Substring

Given a string S, find the longest palindromic substring in S.

You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

{% tabs %}
{% tab title="Scala" %}

```scala
import scala.annotation.tailrec

/**
  * find longest palindrome (length only)
  *
  * @param s
  * @return
  */
def longestPalindromeLength(s: String): Int = {
  @tailrec def check(prev: Int, next: Int, acc: Int): Int = {
    if (prev < 0 || next >= s.length || s(prev) != s(next)) acc
    else check(prev - 1, next + 1, acc + 2)
  }
  var maxLength = 0
  for (i <- 0 until s.length) {
    // check odd length and even length
    maxLength = maxLength max check(i, i, -1) max check(i, i + 1, 0)
  }
  maxLength
}

longestPalindromeLength("")
longestPalindromeLength("a")
longestPalindromeLength("bab")
longestPalindromeLength("baab")

/**
  * find longest palindrome (substring)
  *
  * @param s
  * @return
  */
def longestPalindrome(s: String): String = {
  @tailrec def check(prev: Int, next: Int, acc: Int): Int = {
    if (prev < 0 || next >= s.length || s(prev) != s(next)) acc
    else check(prev - 1, next + 1, acc + 2)
  }
  var maxLength, length, index = 0
  for (i <- 0 until s.length) {
    length = check(i, i, -1) max check(i, i + 1, 0)
    maxLength = maxLength max length
    if (length == maxLength) index = i
  }
  // recover string from index and length
  val diff = maxLength / 2
  maxLength match {
    case l if l % 2 == 0 => s.subSequence(index - diff + 1, index + diff + 1).toString
    case _ => s.subSequence(index - diff, index + diff + 1).toString
  }
}

longestPalindrome("bananas")
longestPalindrome("abracadabra")
```

{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.bernardw.com/problems/longest-palindromic-substring.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
