InferenceAI

Enter at least two characters to search.

Start free

Longest Palindromic Part

mediumArrays & Strings25 min25 XP

Return the longest palindromic substring of s. If several have the maximum length, return the one that starts first.

Requirements

  • O(n2)O(n^2) by expanding around every centre (there are 2n−12n - 1 centres).

Examples

EXAMPLE WALKTHROUGHtwo candidates

A guided view of the published example, not a trace of your code.

INPUT["babad"]
TASKlongest_palindromic_part()
EXPECTED OUTPUT"bab"

Start with the input values shown in this example.

Example 1 of 3

Input: s = "babad"

Output: "bab"

Explanation: two candidates

Input: s = "cbbd"

Output: "bb"

Explanation: even length

Input: s = "a"

Output: "a"

Explanation: single

Hint 1

Expand outwards from each character (odd length) and each gap between characters (even length).

Topics

DSAArrays & StringsPython