DEV Community

dev.to staff
dev.to staff

Posted on

5

Daily Challenge #200 - Longest Linear Palindromic Substring

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!

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay