Longest palindromic substrings: Difference between revisions

Added AutoHotkey
(Added AutoHotkey)
Line 45:
'drome' -> 'e'
'the abbatial palace' -> 'abba'
</pre>
 
=={{header|AutoHotkey}}==
<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
}</lang>
Examples:<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, nl, rt){
result := "[""", i := 0
for i, str in LPS(line)
result .= str """, """
output .= line "`t> " Trim(result, """, """) (i?"""":"") "]`n"
}
MsgBox % output
return</lang>
{{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>
 
299

edits