24
\$\begingroup\$

The shortest code that finds all unique "sub-palindromes" of a string, that is: any substring with length > 1 that is a palindrome.

eg.1

input: "12131331"
output: "33", "121", "131", "313", "1331"

eg.2

input: "3333"
output: "33", "333", "3333"
\$\endgroup\$
  • 1
    \$\begingroup\$ Can a string be it's own sub-palindrome? Since a string is it's own substring. \$\endgroup\$ – JPvdMerwe Jan 29 '11 at 12:17
  • 1
    \$\begingroup\$ @JPvdMerwe: Yes, off course. \$\endgroup\$ – Eelvex Jan 29 '11 at 12:39
  • \$\begingroup\$ Actually more importantly: what must the output of 333 be? Naively you'd end up printing 33 twice \$\endgroup\$ – JPvdMerwe Jan 29 '11 at 12:41
  • \$\begingroup\$ @JPvdMerwe: '333' -> '33', '333'. I'll edit the question accordingly. Thanks. \$\endgroup\$ – Eelvex Jan 29 '11 at 12:53
  • \$\begingroup\$ How is the output specified? Comma-delimited with quotes areound each sub-palindrome as you demonstrate here? One sub-p per line? \$\endgroup\$ – Joey Jan 30 '11 at 12:58

31 Answers 31

1
2
0
\$\begingroup\$

Perl, 43 40 bytes

Includes +1 for n

perl -nE 's/(?=((.)((?1)|.?)\2)(?!.*\1))/say$1/eg' <<< 12131331
| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.