| Bytes | Lang | Time | Link |
|---|---|---|---|
| 008 | Pyth | 250210T070122Z | Glory2Uk |
| 006 | Brachylog | 250210T124418Z | Glory2Uk |
| 244 | Ruby | 250127T191012Z | Pufe |
| 041 | Charcoal | 250124T103646Z | Neil |
| 007 | Vyxal | 250124T071024Z | emanresu |
| 015 | Haskell + hgl | 250124T163530Z | Wheat Wi |
| 008 | 05AB1E | 250124T092627Z | Kevin Cr |
| 007 | Nekomata | 250124T074828Z | alephalp |
| 006 | Vyxal 3 l | 250124T074348Z | lyxal |
| 106 | JavaScript Node.js | 250124T072033Z | l4m2 |
| 105 | ☾ | 250124T065243Z | Used_Bra |
Pyth, 13 8 bytes
h_I#I#./
Old version:
V # iterate through
./Q # the list of partitions
I # if
qQ # the input is equal
s_MN # to the joined reversed partitions
NB # output the substrings combination and break
Ruby, 244 bytes
First* polynomial solution, in O(n²). It is not very golfed besides using one letter variables and removing whitespace.
def f s
n=s.size
m=[[1]]
(1...n).map{|i|
a=[1]
a<<2 if s[i]==s[i-1]
m[i-1].map{|v|a<<v+2 if i>v&&s[i]==s[i-v-1]}
m<<a}
k=[[0,0]]
(0...n).map{|i|
k<<m[i].map{|v|[k[i-v+1][0]+1,v]}.min
}
z=[]
while n>0
y=n-k[n][1]
z.unshift s[y...n]
n=y
end
z
end
Creates a function f that receives the string as parameter and returns an array of strings.
Explanation:
It generates a matrix m with all sizes of palindromes ending on ith letter, then finds the best solution for the first i letters and saves on array k. Finally loops from the end of the string to the beginning using the best partial solutions to construct the actual substrings.
* At least it's the first I've seen...
Charcoal, 53 41 bytes
⊞υ⟦θ⟧Fυ¿¬ⅉ«≔⊟ιη¿ηFΦEη…η⊕λ⁼κ⮌κ⊞υ⁺ι⟦κ✂ηLκ⟧ι
Try it online! Link is to verbose version of code. Explanation:
⊞υ⟦θ⟧
Start a breadth-first search with the input string.
Fυ¿¬ⅉ«
Loop over all the search lists until an answer is found; as this is a breadth-first search it will automatically find the shortest answer first.
≔⊟ιη
Get the current suffix.
¿η
If it is not empty, then...
FΦEη…η⊕λ⁼κ⮌κ
... for each palindromic prefix...
⊞υ⁺ι⟦κ✂ηLκ⟧
... push a new search list with that prefix and a new suffix.
ι
Otherwise this list only contains palindromes, so it's the desired solution.
Vyxal, 7 bytes
øṖ'R⁼;t
øṖ # Partitions
' ; # where
R # reversing each word
⁼ # yields the same list
t # Take the last (= shortest) one
Haskell + hgl, 15 bytes
mBl<<pWK$eq~<Rv
Explanation
This works just like every other answer here. Get the shortest partition that is idempotent under map reverse.
pWKpartitions where each part isrvreverseeqequalitymBlget the shortest
Using parsers, 21 bytes
mBl<<gcy$lA h_>~ʃ<rv
Reflection
This is fine. I'm mostly concerned with improving the parser version.
- There's
glLto get the shortest parse, but there's no builtin to get the shortest (or longest) complete parse. That would save 2 bytes on the parser version:
I could probably even combinegLc$my$lA h_>~ʃ<rvmBlandgcydirectly to save an additional 3 bytes if I was feeling particularly bold. eqgets used so much I'm starting to consider if giving it a 1-byte name is perhaps justified.- A builtin to determine if something is a palindrome (
eq~<Rvcurrently) is probably useful longterm. It would save 4 bytes on the shorter answer.
05AB1E, 8 bytes
.œé.ΔDíQ
Try it online or verify all test cases.
Explanation:
.œ # Get all partitions of the (implicit) input-string
é # Sort these partitions by length (shortest to longest)
.Δ # Then find the first/shortest partition that's truthy for:
D # Duplicate the partition
í # Reverse each inner part of the copy
Q # Check if both lists are still the same
# (after which the found result is output implicitly)
Nekomata, 7 bytes
J:ᵐ↔=aş
J:ᵐ↔=aş
J Find a partition of the input list
: Duplicate
ᵐ↔ Reverse each part
= Check equality
a Get all possible solutions
ş Find the shortest one
Vyxal 3 -l, 6 (literate) bytes
list-partitions filter<
vectorise-reverse ===
} tail
Effectively a port of EmanresuA's Vyxal 2 answer, but independently discovered (minus the tail bit, I originally tried minimum but wasn't sure that'd always return the shortest list)
JavaScript (Node.js), 106 bytes
f=([c,...s],o,g,b,...x)=>c?f(s,f(s,o,g=[g]+c,b=c+[b],...x),'','',...x,...g==b?[g]:g):g||x[o&&o.length]?o:x
JavaScript (Node.js), 114 bytes
f=(s,r=Z='',l='',...a)=>Z=s?[...s].map((c,i)=>(l+=c)==(r=c+r)&&f(s.slice(++i),'','',...a,l))&&Z:Z&&a[Z.length]?Z:a
watever
☾, 34 characters, 105 bytes
2ˣ⭥ᐵ²🃌xᑀ𝌂ᵜ⭥xᐵᵜx⨁ᐸᐸ🃌⋀○≡ᴙ⟞
Note: ☾ uses a custom font, but you can run this code in the Web UI.

Let's set aside a function to check that all strings in a list are palindromes:

The first goal is to get all binary strings with the same length as the input string x:

At this point in the code, we are manipulating the list ["0..00","0..01","0..10",..,"1..11"]
Building on top of this, we want to partition x based on the structure of the binary numbers. For example:
If x="abcde" and we want to partition by "00110", we make a pit stop at [[0,1],[2,3],[4]]

Next, we convert the list with numbers to the string characters at the indices:

Finally, we can finish by getting the first one sorted by length subject to our constraint:
