loading...

Daily Challenge #200 - Longest Linear Palindromic Substring

thepracticaldev profile image dev.to staff ・1 min read

A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., 'madam' or 'racecar'. Even the letter 'x' is considered a palindrome.

You are given a string s. Write a function that returns the longest contiguous palindromic substring in s (it could be the entire string). In the event that there are multiple longest palindromic substrings, return the first to occur.

I'm not trying to trick you here:

You can assume that all inputs are valid strings.
Only the letters a-z will be used, all lowercase (your solution should, in theory, extend to more than just the letters a-z though).
NOTE: Quadratic asymptotic complexity (O(N^2)) or above will NOT work here.

Examples

Basic Tests
Input: "babad"
Output: "bab"
(Note: "bab" occurs before "aba")

Input: "abababa"
Output: "abababa"

Input: "cbbd"
Output: "bb"

Edge Cases
Input: "ab"
Output: "a"

Input: ""
Output: ""

Tests

longest_palindrome('banana')
longest_palindrome('abba')
longest_palindrome('cbbd')
longest_palindrome('zz')
longest_palindrome('dddd')

Good Luck!


This challenge comes from wyatt00 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Posted on by:

thepracticaldev profile

dev.to staff

@thepracticaldev

The hardworking team behind dev.to ❤️

Discussion

pic
Editor guide