Longest Palindromic Substring
Find the longest contiguous substring of a 74-character lowercase string that reads the same forwards and backwards.
A1 contains a 74-character lowercase string — a transmission with one
embedded palindromic callsign hidden in noise. Return the longest
contiguous substring that reads the same forwards and backwards.
The string has C(74, 2) = 2,701 substrings of length ≥ 2. Eyeballing that for symmetry doesn’t work — the formula has to enumerate, test, and pick the max. For this input the longest palindrome is unique.
Input
Range A1
| A | |
|---|---|
| 1 | xojbnpxmuilgelneugukbmfqqiwgusumamenodonemamusfbtkwfcujjgvzlhtkzvxoevnwenw |
Hint
Generate every substring with MAKEARRAY (rows = start, cols = end), flatten with TOCOL skipping blanks, BYROW-test each one with a palindrome checker, then FILTER to the longest.
Solution
=LET( str, A1, n, LEN(str), rev, LAMBDA(txt, LET(ln,LEN(txt), CONCAT(MID(txt,SEQUENCE(ln,,ln,-1),1)) = txt)), m, TOCOL(MAKEARRAY(n, n, LAMBDA(r,c, IF(AND(c>r,c<>r), MID(str,r,c-r+1), ""))), 3), b, BYROW(m, LAMBDA(br, IFERROR(rev(INDEX(br,1,1)), FALSE))), palindromes, FILTER(m, b*LEN(m)), FILTER(palindromes, LEN(palindromes) = MAX(LEN(palindromes))) )