Skip to main content
added 901 characters in body
Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975

CJam, 26 bytes

LLq{+_a2$&{a+L}|}/:X+X!S**

Test it here.

Explanation

L        e# Push an empty array to keep track if the previous segments.
L        e# Push an empty array to build the current segment.
q{       e# For each character in the input...
  +      e#   Add it to the current segment.
  _a2$&  e#   Duplicate and check if it's already in the segment list.
  {      e#   If not...
    a+L  e#     Add it to the list and push a new empty array for the next segment.
  }|
}/
:X+      e# Store the trailing segment in X and add it's *characters* to the list.
         e# For valid splittings, this trailing segment will be empty, so that the
         e# list remains unchanged.
X!       e# Push X again and take the logical NOT.
S*       e# Get that many spaces, i.e. 1 for valid segments and 0 otherwise.
*        e# Join the list with this string between elements.

CJam, 26 bytes

LLq{+_a2$&{a+L}|}/:X+X!S**

Test it here.

CJam, 26 bytes

LLq{+_a2$&{a+L}|}/:X+X!S**

Test it here.

Explanation

L        e# Push an empty array to keep track if the previous segments.
L        e# Push an empty array to build the current segment.
q{       e# For each character in the input...
  +      e#   Add it to the current segment.
  _a2$&  e#   Duplicate and check if it's already in the segment list.
  {      e#   If not...
    a+L  e#     Add it to the list and push a new empty array for the next segment.
  }|
}/
:X+      e# Store the trailing segment in X and add it's *characters* to the list.
         e# For valid splittings, this trailing segment will be empty, so that the
         e# list remains unchanged.
X!       e# Push X again and take the logical NOT.
S*       e# Get that many spaces, i.e. 1 for valid segments and 0 otherwise.
*        e# Join the list with this string between elements.
Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975

CJam, 26 bytes

LLq{+_a2$&{a+L}|}/:X+X!S**

Test it here.