5
A palindrome is a string which is the same when read forwards and backwards. For example "racecar" is a palindrome, but "Racecar" is not. A substring is a contiguous set of characters in a larger string. Your task is to write a program or method which takes a string or character array as input and outputs the length of the longest substring of that string which is a palindrome.
Examples
Input
banana
Ouput
5
This is because "anana" is a 5 character substring of "banana"
Input
abracadabra
Output
3
This is because both "aca" and "ada" are 3 characters.
Input
True
Output
1
This string has no palindromes that are more than 1 character.
If the input is the empty string the output must be 0.
This is code-golf so the shortest submission in any language wins.
Note: There have been slight variations of this question in the past, which have all had wording issues or been asking a variation of this question. You can see these:
Find the Longest Palindrome in a String by Removing Characters
Longest reverse palindromic DNA substring
How do I find the longest palindrome in a string?
Finding "sub-palindromes". - The purpose of this challenge is to print all palindromic substring, rather than the length of the longest. As such many of the answers to this question do not have close analogues.
2Related, borderline dupe. My vote is a hammer, though, so I won't vote as yet. – AdmBorkBork – 2018-02-13T13:43:58.787
https://codegolf.stackexchange.com/questions/16327/how-do-i-find-the-longest-palindrome-in-a-string is another, but it's not really another. – Magic Octopus Urn – 2018-02-13T13:50:16.023
@MagicOctopusUrn only the size of it. After all the longest palindrome might not be unique. – Bijan – 2018-02-13T14:00:58.603
Which are the valid input characters ? – Ton Hospel – 2018-02-13T14:07:43.207
@TonHospel The original idea was that it would just be what ever the string type supports. Did you have something in mind? – Bijan – 2018-02-13T14:10:50.880
The linked challenge is asking for unique sub-palindromes. There is no such constraint here, so converting these answers is likely to be sub-optimal. (This still is a borderline dupe, though.) – Arnauld – 2018-02-13T15:05:14.033
2@Bijan I usually golf this kind of thing using regexes. And if I substitute a string into a regex it's important to know if the characters can be regex metacharacters (like
.
,+
,*
etc) because just using them as is will cause errors. There is also the issue of spaces and newlines and how they are handled in I/O. Languages likeC
might worry about\0
etc. – Ton Hospel – 2018-02-13T16:03:13.003Given that most answers there can be ported here with "Length Each Max" appended (and it looks like that's a common approach), I think it can be considered a dupe. – user202729 – 2018-02-14T01:23:29.493
@user202729 At least in perl it's not that easy since perl doesn't have a cheap
max
orsort
so it's a bit different (not by much though, since I did find a close workaround). The simplemap length and max
is mostly for the golfing languages I think – Ton Hospel – 2018-02-14T10:27:52.220