Longest palindromic substrings: Difference between revisions
Added Easylang
(Added Easylang) |
|||
(28 intermediate revisions by 16 users not shown) | |||
Line 6:
* [[Palindrome_detection]]
{{Template:Strings}}
<br><br>
=={{header|11l}}==
<syntaxhighlight lang="11l">F longest_palindrome(s)
V t = Array(‘^’s‘$’).join(‘#’)
V n = t.len
V p = [0] * n
V c = 0
V r = 0
L(i) 1 .< n - 1
p[i] = (r > i) & min(r - i, p[2 * c - i]) != 0
L t[i + 1 + p[i]] == t[i - 1 - p[i]]
p[i]++
I i + p[i] > r
(c, r) = (i, i + p[i])
V (max_len, center_index) = max(enumerate(p).map((i, n) -> (n, i)))
R s[(center_index - max_len) I/ 2 .< (center_index + max_len) I/ 2]
L(s) [‘three old rotators’,
‘never reverse’,
‘stable was I ere I saw elbatrosses’,
‘abracadabra’,
‘drome’,
‘the abbatial palace’]
print(‘'’s‘' -> '’longest_palindrome(s)‘'’)</syntaxhighlight>
{{out}}
<pre>
'three old rotators' -> 'rotator'
'never reverse' -> 'ever reve'
'stable was I ere I saw elbatrosses' -> 'table was I ere I saw elbat'
'abracadabra' -> 'ada'
'drome' -> 'e'
'the abbatial palace' -> 'abba'
</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">BYTE FUNC Palindrome(CHAR ARRAY s)
BYTE l,r
l=1 r=s(0)
WHILE l<r
DO
IF s(l)#s(r) THEN RETURN (0) FI
l==+1 r==-1
OD
RETURN (1)
PROC Find(CHAR ARRAY text,res)
BYTE first,len
len=text(0)
WHILE len>0
DO
FOR first=1 TO text(0)-len+1
DO
SCopyS(res,text,first,first+len-1)
IF Palindrome(res) THEN
RETURN
FI
OD
len==-1
OD
res(0)=0
RETURN
PROC Test(CHAR ARRAY text)
CHAR ARRAY res(100)
Find(text,res)
PrintF("""%S"" -> ""%S""%E",text,res)
RETURN
PROC Main()
Test("three old rotators")
Test("never reverse")
Test("abracadabra")
Test("the abbatial palace")
Test("qwertyuiop")
Test("")
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Longest_palindromic_substrings.png Screenshot from Atari 8-bit computer]
<pre>
"three old rotators" -> "rotator"
"never reverse" -> "ever reve"
"abracadabra" -> "aca"
"the abbatial palace" -> "abba"
"qwertyuiop" -> "q"
"" -> ""
</pre>
=={{header|ALGOL 68}}==
Palindromes of length 1 or more are detected, finds the left most palindrome if there are several of the same length.<br>
Treats upper and lower case as distinct and does not require the characters to be letters.
<syntaxhighlight lang="algol68">BEGIN # find the longest palindromic substring of a string #
# returns the length of s #
OP LENGTH = ( STRING s )INT: ( UPB s + 1 ) - LWB s;
# returns s right-padded with blanks to at least w characters or s if it is already wide enough #
PRIO PAD = 1;
OP PAD = ( STRING s, INT w )STRING:
BEGIN
STRING result := s;
WHILE LENGTH result < w DO result +:= " " OD;
result
END # PAD # ;
# returns the longest palindromic substring of s #
# if there are multiple substrings with the longest length, the leftmost is returned #
PROC longest palindromic substring = ( STRING s )STRING:
IF LENGTH s < 2
THEN s
ELSE
INT lwb s = LWB s;
INT upb s = UPB s;
STRING result := s[ lwb s ];
IF s[ lwb s + 1 ] = s[ lwb s ] THEN
# the first two characters are a palindrome #
result +:= s[ lwb s + 1 ]
FI;
FOR i FROM lwb s + 1 TO upb s - 1 DO
INT p start := i;
INT p end := i + 1;
IF IF s[ i - 1 ] = s[ i + 1 ] THEN
# odd length palindrome at i - 1 #
p start -:= 1;
TRUE
ELIF s[ i ] = s[ i + 1 ] THEN
# even length palindrome at i #
TRUE
ELSE FALSE
FI
THEN
# have a palindrome at p start : p end #
# attempt to enlarge the range #
WHILE IF p start = lwb s OR p end = upb s
THEN FALSE
ELSE s[ p start - 1 ] = s[ p end + 1 ]
FI
DO # can extend he palindrome #
p start -:= 1;
p end +:= 1
OD;
IF ( p end + 1 ) - p start > LENGTH result
THEN
# have a longer palindrome #
result := s[ p start : p end ]
FI
FI
OD;
result
FI # longest palindromic substring # ;
# finds the longest palindromic substring of s and checks it is the expacted value #
PROC test = ( STRING s, expected value )VOID:
BEGIN
STRING palindrome = longest palindromic substring( s );
print( ( ( """" + s + """" ) PAD 38, " -> ", ( """" + palindrome + """" ) PAD 36
, IF palindrome = expected value THEN "" ELSE " NOT " + expected value + " ???" FI
, newline
)
)
END # test longest palindromic substring # ;
test( "three old rotators", "rotator" );
test( "never reverse", "ever reve" );
test( "stable was I ere I saw elbatrosses", "table was I ere I saw elbat" );
test( "abracadabra", "aca" );
test( "drome", "d" );
test( "x", "x" );
test( "the abbatial palace", "abba" );
test( "", "" );
test( "abcc", "cc" );
test( "abbccc", "ccc" )
END</syntaxhighlight>
{{out}}
<pre>
"three old rotators" -> "rotator"
"never reverse" -> "ever reve"
"stable was I ere I saw elbatrosses" -> "table was I ere I saw elbat"
"abracadabra" -> "aca"
"drome" -> "d"
"x" -> "x"
"the abbatial palace" -> "abba"
"" -> ""
"abcc" -> "cc"
"abbccc" -> "ccc"
</pre>
=={{header|Arturo}}==
{{trans|Nim}}
<syntaxhighlight lang="rebol">palindrome?: function [str]-> str = join reverse split str
lps: function [s][
maxLength: 0
result: new []
loop 0..dec size s 'fst [
loop fst..dec size s 'lst [
candidate: slice s fst lst
if palindrome? candidate [
if? maxLength < size candidate [
result: new @[candidate]
maxLength: size candidate
]
else [
if maxLength = size candidate ->
'result ++ candidate
]
]
]
]
return (maxLength > 1)? -> result
-> []
]
loop ["babaccd", "rotator", "several", "palindrome", "piété", "tantôt", "étêté"] 'str [
palindromes: lps str
print [str "->" (0 < size palindromes)? -> join.with:", " palindromes
-> "X"]
]</syntaxhighlight>
{{out}}
<pre>babaccd -> bab, aba
rotator -> rotator
several -> eve
palindrome -> X
piété -> été
tantôt -> tôt
étêté -> étêté</pre>
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">LPS(str){
found := [], result := [], maxL := 0
while (StrLen(str) >= 2 && StrLen(str) >= maxL){
s := str
loop {
while (SubStr(s, 1, 1) <> SubStr(s, 0)) ; while 1st chr <> last chr
s := SubStr(s, 1, StrLen(s)-1) ; trim last chr
if (StrLen(s) < 2 || StrLen(s) < maxL )
break
if (s = reverse(s)){
found.Push(s)
maxL := maxL < StrLen(s) ? StrLen(s) : maxL
break
}
s := SubStr(s, 1, StrLen(s)-1) ; trim last chr
}
str := SubStr(str, 2) ; trim 1st chr and try again
}
maxL := 0
for i, str in found
maxL := maxL < StrLen(str) ? StrLen(str) : maxL
for i, str in found
if (StrLen(str) = maxL)
result.Push(str)
return result
}
reverse(s){
for i, v in StrSplit(s)
output := v output
return output
}</syntaxhighlight>
Examples:<syntaxhighlight lang="autohotkey">db =
(
three old rotators
never reverse
stable was I ere I saw elbatrosses
abracadabra
drome
x
the abbatial palace
)
for i, line in StrSplit(db, "`n", "`r"){
result := "[""", i := 0
for i, str in LPS(line)
result .= str """, """
output .= line "`t> " Trim(result, """, """) (i?"""":"") "]`n"
}
MsgBox % output
return</syntaxhighlight>
{{out}}
<pre>three old rotators > ["rotator"]
never reverse > ["ever reve"]
stable was I ere I saw elbatrosses > ["table was I ere I saw elbat"]
abracadabra > ["aca", "ada"]
drome > []
x > []
the abbatial palace > ["abba"]
</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
func$ reverse s$ .
a$[] = strchars s$
for i = 1 to len a$[] div 2
swap a$[i] a$[len a$[] - i + 1]
.
return strjoin a$[]
.
func palin s$ .
if s$ = reverse s$
return 1
.
return 0
.
func$ lpali st$ .
for n = 1 to len st$ - 1
for m = n + 1 to len st$
sub$ = substr st$ n (m - n)
if palin sub$ = 1
if len sub$ > len max$
max$ = sub$
.
.
.
.
return max$
.
for s$ in [ "three old rotators" "never reverse" "stable was I ere I saw elbatrosses" "abracadabra" "drome" "the abbatial palace" ]
print lpali s$
.
</syntaxhighlight>
{{out}}
<pre>
rotator
ever reve
table was I ere I saw elbat
aca
d
abba
</pre>
=={{header|F_Sharp|F#}}==
===Manacher Function===
<
// Manacher Function. Nigel Galloway: October 1st., 2020
let Manacher(s:string) = let oddP,evenP=Array.zeroCreate s.Length,Array.zeroCreate s.Length
Line 25 ⟶ 360:
match (Ʃ+evenP.[Ʃ])>g with true->fGe (Ʃ-evenP.[Ʃ]+1) (Ʃ+evenP.[Ʃ]) (Ʃ+1) |_->fGe n g (Ʃ+1)
(fGo 0 -1 0,fGe 0 -1 0)
</syntaxhighlight>
===The Task===
<
let fN g=if g=[||] then (0,0) else g|>Array.mapi(fun n g->(n,g))|>Array.maxBy snd
let lpss s=let n,g=Manacher s in let n,g=fN n,fN g in if (snd n)*2+1>(snd g)*2 then s.[(fst n)-(snd n)..(fst n)+(snd n)] else s.[(fst g)-(snd g)+1..(fst g)+(snd g)]
let test = ["three old rotators"; "never reverse"; "stable was I ere I saw elbatrosses"; "abracadabra"; "drome"; "the abbatial palace"; ""]
test|>List.iter(fun n->printfn "A longest palindromic substring of \"%s\" is \"%s\"" n (lpss n))
</syntaxhighlight>
{{out}}
<pre>
Line 44 ⟶ 379:
A longest palindromic substring of "" is ""
</pre>
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">Function isPalindrome(s As String) As Integer
For i As Integer = 1 To Len(s) / 2
If Mid(s, i, 1) <> Mid(s, Len(s) - i + 1, 1) Then Return False
Next i
Return True
End Function
Sub LongestPalindrome(s As String)
Dim As String substr, longest = ""
Dim As Integer i, j
For i = 1 To Len(s)
For j = i To Len(s)
substr = Mid(s, i, j - i + 1)
If isPalindrome(substr) Andalso Len(substr) > Len(longest) Then longest = substr
Next j
Next i
Print "The longest palindromic substring is/are: "
For i = 1 To Len(s)
For j = i To Len(s)
substr = Mid(s, i, j - i + 1)
If IsPalindrome(substr) Andalso Len(substr) = Len(longest) Andalso Len(substr) > 2 Then Print substr; " ";
Next j
Next i
If Len(longest) <= 2 Then Print "<no palindromic substring of two of more letters found>"
End Sub
Dim s As String
Input "Enter a string: ", s
LongestPalindrome(s)
Sleep</syntaxhighlight>
=={{header|Go}}==
{{trans|Wren}}
<
import (
Line 113 ⟶ 483:
fmt.Printf(" %-13s Length %d -> %v\n", s, len(longest[0]), longest)
}
}</
{{out}}
Line 130 ⟶ 500:
A list version, written out of curiosity. A faster approach could be made with an indexed datatype.
<
longestPalindromes :: String -> ([String], Int)
Line 187 ⟶ 557:
where
rjust n c = drop . length <*> (replicate n c ++)
w = maximum (length . xShow <$> xs)</
{{Out}}
<pre>Longest palindromic substrings:
Line 198 ⟶ 568:
"the abbatial palace" -> (["abba"],4)
"" -> ([],0)</pre>
=={{header|jq}}==
'''Adapted from [[#Wren]]'''
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">def longestPalindromicSubstring:
length as $len
| if $len <= 1 then .
else explode as $s
| {targetLen: $len, longest: [], i: 0}
| until(.stop;
(.i + .targetLen - 1) as $j
| if $j < $len
then $s[.i:$j+1] as $ss
| if $ss == ($ss|reverse) then .longest += [$ss] else . end
| .i += 1
else
if .longest|length > 0 then .stop=true else . end
| .i = 0
| .targetLen += - 1
end )
| .longest
| map(implode)
| unique
end ;
def strings:
["babaccd", "rotator", "reverse", "forever", "several", "palindrome", "abaracadaraba"];
"The palindromic substrings having the longest length are:",
(strings[]
| longestPalindromicSubstring as $longest
| " \(.): length \($longest[0]|length) -> \($longest)"
)</syntaxhighlight>
{{out}}
<pre>
The palindromic substrings having the longest length are:
babaccd: length 3 -> ["aba","bab"]
rotator: length 7 -> ["rotator"]
reverse: length 5 -> ["rever"]
forever: length 5 -> ["rever"]
several: length 3 -> ["eve"]
palindrome: length 1 -> ["a","d","e","i","l","m","n","o","p","r"]
abaracadaraba: length 3 -> ["aba","aca","ada","ara"]
</pre>
=={{header|Julia}}==
<
list, len = String[], length(s)
for i in 1:len-1, j in i+1:len
Line 217 ⟶ 634:
join(list[findall(x -> length(x) == length(list[end]), list)], "\" or \""), "\"")
end
</
<pre>
The longest palindromic substring of babaccd is: "bab" or "aba"
Line 228 ⟶ 645:
===Manacher algorithm===
<
function manacher(str)
s = "^" * join(split(str, ""), "#") * "\$"
Line 253 ⟶ 670:
"The longest palindromic substring of $teststring is: \"$pal\"")
end
</
<pre>
The longest palindromic substring of babaccd is: "aba"
Line 264 ⟶ 681:
</pre>
=={{header|
<syntaxhighlight lang="mathematica">ClearAll[ExpandSubsequenceTry, LongestPalindromicSubsequence]
ExpandSubsequenceTry[seq_List, beginpos : {a_, b_}] :=
Module[{len, maxbroaden, last},
len = Length[seq];
maxbroaden = Min[a - 1, len - b];
last = maxbroaden;
Do[
If[! PalindromeQ[Take[seq, {a - j, b + j}]],
last = j - 1;
Break[];
]
,
{j, maxbroaden}
];
{a - last, b + last}
]
LongestPalindromicSubsequence[l_List] :=
Module[{evenposs, oddposs, subseqs},
evenposs = SequencePosition[l, {x_, x_}];
oddposs = SequencePosition[l, {x_, y_, x_}];
subseqs = Join[evenposs, oddposs];
subseqs = ExpandSubsequenceTry[l, #] & /@ subseqs;
If[Length[subseqs] > 0,
TakeLargestBy[Take[l, #] & /@ subseqs, Length, 1][[1]]
,
{}
]
]
StringJoin@LongestPalindromicSubsequence[Characters["three old rotators"]]
StringJoin@LongestPalindromicSubsequence[Characters["never reverse"]]
StringJoin@LongestPalindromicSubsequence[Characters["stable was I ere I saw elbatrosses"]]
StringJoin@LongestPalindromicSubsequence[Characters["abracadabra"]]
StringJoin@LongestPalindromicSubsequence[Characters["drome"]]
StringJoin@LongestPalindromicSubsequence[Characters["the abbatial palace"]]</syntaxhighlight>
{{out}}
<pre>"rotator"
"ever reve"
"table was I ere I saw elbat"
"aca"
""
"abba"</pre>
=={{header|Nim}}==
Simple algorithm but working on Unicode code points.
<syntaxhighlight lang="nim">import sequtils, strutils, unicode
func isPalindrome(s: seq[Rune]): bool =
## Return true if a sequence of runes is a palindrome.
for i in 1..(s.len shr 1):
if s[i - 1] != s[^i]:
return false
result = true
func lps(s: string): seq[string] =
var maxLength = 0
var list: seq[seq[Rune]]
let r = s.toRunes
for first in 0..r.high:
for last in first..r.high:
let candidate = r[first..last]
if candidate.isPalindrome():
if candidate.len > maxLength:
list = @[candidate]
maxLength = candidate.len
elif candidate.len == maxLength:
list.add candidate
if maxLength > 1:
result = list.mapIt($it)
for str in ["babaccd", "rotator", "several", "palindrome", "piété", "tantôt", "étêté"]:
let result = lps(str)
if result.len == 0:
echo str, " → ", "<no palindromic substring of two of more letters found>"
else:
echo str, " → ", result.join(", ")</syntaxhighlight>
{{out}}
<pre>babaccd → bab, aba
rotator → rotator
several → eve
palindrome → <no palindromic substring of two of more letters found>
piété → été
tantôt → tôt
étêté → étêté</pre>
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
program FindLongestPalindrome;
uses
SysUtils,strutils;
const
arr: array of string = ('three old rotators', 'never reverse', 'stable was I ere I saw elbatrosses', 'abracadabra', 'drome', 'the abbatial palace', '');
var
st, longestPalindrome, dummy: string;
i, j, longest: integer;
begin
for st in arr do
begin
longest := 0;
longestPalindrome := '';
for i := 1 to Length(st) do
begin
for j := Length(st) downto i do
begin
dummy := Copy(st, i, j - i + 1);
if (j - i + 1 > longest) and (dummy = ReverseString(dummy)) then
begin
longest := j - i + 1;
longestPalindrome := dummy;
end;
end;
end;
WriteLn(Format('%-35s -> %s', [st, longestPalindrome]));
end;
end.
</syntaxhighlight>
{{out}}
<pre>
three old rotators -> rotator
never reverse -> ever reve
stable was I ere I saw elbatrosses -> table was I ere I saw elbat
abracadabra -> aca
drome -> d
the abbatial palace -> abba
->
</pre>
=={{header|Perl}}==
The short one - find all palindromes with one regex.
<syntaxhighlight lang="perl">use strict;
use warnings;
print "Longest Palindrome For $_ = @{[ longestpalindrome($_) ]}\n"
for qw(babaccd rotator reverse forever several palindrome abaracadabra);
sub longestpalindrome
{
my @best = {"''" => 0};
pop =~ /(.+) .? (??{reverse $1}) (?{ $best[length $&]{$&}++ }) (*FAIL)/x;
keys %{pop @best};
}</syntaxhighlight>
{{out}}
<pre>
Longest Palindrome For babaccd = aba bab
Longest Palindrome For rotator = rotator
Longest Palindrome For reverse = rever
Longest Palindrome For forever = rever
Longest Palindrome For several = eve
Longest Palindrome For palindrome = ''
Longest Palindrome For abaracadabra = aba ara aca ada
</pre>
The faster one - does the million digits of Pi in under half a second.
<syntaxhighlight lang="perl">use strict;
use warnings;
use feature 'bitwise';
#@ARGV = 'pi.dat'; # uncomment to use this file or add filename to command line
my $forward = lc do { local $/; @ARGV ? <> : <DATA> };
$forward =~ s/\W+//g;
my $range = 10;
my $backward = reverse $forward;
my $length = length $forward;
my @best = {"''" => 0};
my $len;
for my $i ( 1 .. $length - 2 )
{
do
{
my $right = substr $forward, $i, $range;
my $left = substr $backward, $length - $i, $range;
( $right ^. $left ) =~ /^\0\0+/ and # evens
($len = 2 * length $&) >= $#best and
$best[ $len ]{substr $forward, $i - length $&, $len}++;
( $right ^. "\0" . $left ) =~ /^.(\0+)/ and # odds
($len = 1 + 2 * length $1) >= $#best and
$best[ $len ]{substr $forward, $i - length $1, $len}++;
} while $range < $#best and $range = $#best;
}
print "Longest Palindrome ($#best) : @{[ keys %{ $best[-1] } ]}\n";
__DATA__
this data borrowed from raku...
Never odd or even
Was it a car or a cat I saw?
Too bad I hid a boot
I, man, am regal - a German am I
toot
Warsaw was raw</syntaxhighlight>
{{out}}
<pre>
Longest Palindrome (27) : ootimanamregalagermanamitoo
</pre>
=={{header|Phix}}==
{{trans|Raku}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo/rosetta/Longest_palindromic_substrings.exw (plus two older versions)</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">longest_palindromes</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- s = lower/strip_spaces_and_punctuation/utf8_to_utf32, if rqd</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">longest</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span> <span style="color: #000080;font-style:italic;">-- (do not treat length 1 as palindromic)
-- integer longest = 1 -- (do not treat length 0 as palindromic) [works just fine too]</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]?</span><span style="color: #000000;">2</span><span style="color: #0000FF;">:</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">rev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">fwd</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">rev</span><span style="color: #0000FF;"><</span><span style="color: #000000;">i</span> <span style="color: #008080;">and</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">fwd</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">rev</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">fwd</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">rev</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">fwd</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">rev</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">fwd</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">lp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">lp</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">longest</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">lp</span><span style="color: #0000FF;">></span><span style="color: #000000;">longest</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">longest</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lp</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">p</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">elsif</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (or just "else")</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span> <span style="color: #000080;font-style:italic;">-- (or "sort(res)" or "unique(res)", as needed)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"babaccd"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"rotator"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"reverse"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"forever"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"several"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"palindrome"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"abaracadaraba"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"abbbc"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s: %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">longest_palindromes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 302 ⟶ 932:
palindrome: {}
abaracadaraba: {"aba","ara","aca","ada"}
abbbc: {"bbb"}
</pre>
with longest initialised to 1, you get the same except for <code>palindrome: {"p","a","l","i","n","d","r","o","m","e"}</code>
=={{header|Python}}==
Line 413 ⟶ 942:
(This version ignores case but allows non-alphanumerics).
<
Line 546 ⟶ 1,075:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>Longest palindromic substrings:
Line 562 ⟶ 1,091:
This version regularizes (ignores) case and ignores non alphanumeric characters. It is only concerned with finding the ''longest'' palindromic substrings so does not exhaustively find ''all possible'' palindromes. If a palindromic substring is found to be part of a longer palindrome, it is not captured separately. Showing the longest 5 palindromic substring groups. Run it with no parameters to operate on the default; pass in a file name to run it against that instead.
<syntaxhighlight lang="raku"
Lyrics to "Bob" copyright Weird Al Yankovic
https://www.youtube.com/watch?v=JUQDzj6R3p4
Line 610 ⟶ 1,139:
#"
my @cpfoa
(1 ..^ @chars).race(:1000batch).map: -> \idx {
for 1, 2 {
my int ($rev, $fwd) = $_, 1;
quietly last if ($rev > idx) || (@chars[idx - $rev] ne @chars[idx + $fwd]);
$fwd = $fwd + 1;
}
@s.push: @chars[idx - $rev
}
next unless +@s;
}
"{.key} ({+.value})\t{.value.unique.sort}".put for @cpfoa.
{{out}}
Returns the length, (the count) and the list:
<pre>29 (2) doninemeninterpretninemeninod godarednuggetafateggunderadog
26 (1) gohangasalamiimalasagnahog
23 (1) arwontloversrevoltnowra
21 (4) imanamregalagermanami mayamoodybabydoomayam ootnolemonsnomelontoo oozyratinasanitaryzoo
20 (1) ratsliveonnoevilstar
</pre>
This isn't intensively optimised but isn't too shabby either. When run against the first million digits of pi: [
<pre>13 (1) 9475082805749
12 (1) 450197791054
11 (8) 04778787740 09577577590 21348884312 28112721182 41428782414 49612121694 53850405835 84995859948
10 (9) 0045445400 0136776310 1112552111 3517997153 5783993875 6282662826 7046006407 7264994627 8890770988
9 (98) 019161910 020141020 023181320 036646630 037101730 037585730 065363560 068363860 087191780 091747190 100353001 104848401 111262111 131838131 132161231 156393651 160929061 166717661 182232281 193131391 193505391 207060702 211878112 222737222 223404322 242424242 250171052 258232852 267919762 272636272 302474203 313989313 314151413 314424413 318272813 323212323 330626033 332525233 336474633 355575553 357979753 365949563 398989893 407959704 408616804 448767844 450909054 463202364 469797964 479797974 480363084 489696984 490797094 532121235 546000645 549161945 557040755 559555955 563040365 563828365 598292895 621969126 623707326 636414636 636888636 641949146 650272056 662292266 667252766 681565186 684777486 712383217 720565027 726868627 762727267 769646967 777474777 807161708 819686918 833303338 834363438 858838858 866292668 886181688 895505598 896848698 909565909 918888819 926676629 927202729 929373929 944525449 944848449 953252359 972464279 975595579 979202979 992868299</pre>
in
=={{header|REXX}}==
<
parse arg s /*obtain optional argument from the CL.*/
if s==''
/* [↑] the case of strings is respected*/
do LL=2 for L-1 /*start with palindromes of length two.*/
if
end
LL= max(1, m)
call find 0 /*find all palindromes with length LL.*/
say ' longest palindromic substrings for string: ' x
say '────────────────────────────────────────────'copies('─', 2 + L)
do n=1 for words(@) /*show longest palindromic substrings. */
say '
end /*n*/; say;
end
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
find: parse arg short /*if SHORT==1, only find 1
@= /*initialize palindrome list to a null.*/
@= @ $
if short then
end /*j*/; return 0
{{out|output|text= when using the default input:}}
<pre>
Line 728 ⟶ 1,255:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 757 ⟶ 1,284:
see "Longest palindromic substrings:" + nl
see resList
</syntaxhighlight>
{{out}}
<pre>
Line 772 ⟶ 1,299:
The Phix entry examples have been used.
<
import "./fmt" for Fmt
var longestPalSubstring = Fn.new { |s|
Line 800 ⟶ 1,327:
var longest = Lst.distinct(longestPalSubstring.call(s))
Fmt.print(" $-13s Length $d -> $n", s, longest[0].count, longest)
}</
{{out}}
|