# Downgrade to a Palindrome

## J, 24 bytes

```
(0{::(-:|.)\.#&,<\)~i.@#
```

Try it online!

### Explanation

```
(0{::(-:|.)\.#&,<\)~i.@# Input: array of chars S
# Length of S
i.@ Range, [0, 1, ..., len(S)-1]
( )~ Dyadic verb on range and S
\. For each outfix of S of size x in range
|. Reverse
-: Matches input (is palindrome)
<\ Box each infix of S of size x in range
#&, Flatten each and copy the ones that match
0{:: Fetch the result and index 0 and return
```

## Jelly, 16 bytes

```
Ḣ;Ṫµ=Ṛ
0,0jŒṖÇÞṪ
```

Try it online!

### How it works

```
0,0jŒṖÇÞṪ Main link. Argument: s (string)
0,0j Join [0, 0], separating by s. This prepends and appends a 0 to s.
ŒṖ Build all partitions of the resulting array.
ÇÞ Sort the partitions by the helper link.
As a side effect, this will remove the first and last element of each
partition. The 0's make sure that not removing any characters from s
will still remove [0] from both sides.
Ṫ Tail; extract the last one.
Ḣ;Ṫµ=Ṛ Helper link. Argument: A (array/partition)
Ḣ Head; yield and remove the first chunk of A.
Ṫ Tail; yield and remove the last chunk of A.
; Concatenate head and tail.
µ=Ṛ Compare the result, character by character, with its reverse.
A palindrome of length l will yield an array of l 1's, while a
non-palindrome of length l will yield an array with at least one 0 among
the first l/2 Booleans. The lexicographically largest result is the one
with the longest prefix of 1's, which corresponds to the longest
palindrome among the outfixes.
```

### Wolfram Language (Mathematica), ~~53~~ 51 bytes

Byte count assumes CP-1252 encoding.

```
±{a___,Shortest@b___,c___}/;PalindromeQ[a<>c]:={b}
```

Try it online!

Defines a unary operator `±`

(or a function `PlusMinus`

). Input and output are lists of characters. The test suite does the conversion from and to actual strings for convenience.