Skip to main content
Golfed 2 bytes
Source Link
DLosc
  • 39.2k
  • 5
  • 83
  • 141

Brachylog, 1212 10 bytes

-2 bytes thanks to Fatalize

Ẹ|k↰;Ė|k↰;?tᵗ↺↙Ḋctᵗpc

A predicate that takes the list of characters \$\sigma\$ as its input parameter and the string \$S\$ as its output parameter, succeeding if \$S\$ is \$\sigma\$-writable and failing if it is not. (Both \$\sigma\$ and \$S\$ are taken as Brachylog strings.) Try it online!Try it online!

Explanation

The predicate is recursive:

Ẹ|k↰;Ė|k↰;?tᵗ↺↙Ḋctᵗpc              Base case: input and output are both the empty string
 |             Or (recursive case):
   ↰           Call this predicate recursively on
  k            The input with its last character removed
    ;?tᵗ       Put the result in a list with the original input's last character
        ↺↙Ḋp    Try both orderspermutations of that list
            c   Concatenate the elements into a single string
               which must match the output parameter

The ↺↙Ḋ idiom means "rotate the list counterclockwise between 0 and 9 times," which is less efficient but one byte shorter than the more-obvious {|↔} ("the list as-is or reversed").

Brachylog, 12 bytes

Ẹ|k↰;?tᵗ↺↙Ḋc

A predicate that takes the list of characters \$\sigma\$ as its input parameter and the string \$S\$ as its output parameter, succeeding if \$S\$ is \$\sigma\$-writable and failing if it is not. (Both \$\sigma\$ and \$S\$ are taken as Brachylog strings.) Try it online!

Explanation

The predicate is recursive:

Ẹ|k↰;?tᵗ↺↙Ḋc              Base case: input and output are both the empty string
 |             Or (recursive case):
   ↰           Call this predicate recursively on
  k            The input with its last character removed
    ;?tᵗ       Put the result in a list with the original input's last character
        ↺↙Ḋ   Try both orders of that list
            c  Concatenate the elements into a single string
               which must match the output parameter

The ↺↙Ḋ idiom means "rotate the list counterclockwise between 0 and 9 times," which is less efficient but one byte shorter than the more-obvious {|↔} ("the list as-is or reversed").

Brachylog, 12 10 bytes

-2 bytes thanks to Fatalize

Ė|k↰;?tᵗpc

A predicate that takes the list of characters \$\sigma\$ as its input parameter and the string \$S\$ as its output parameter, succeeding if \$S\$ is \$\sigma\$-writable and failing if it is not. (Both \$\sigma\$ and \$S\$ are taken as Brachylog strings.) Try it online!

Explanation

The predicate is recursive:

Ė|k↰;?tᵗpc
Ẹ            Base case: input and output are both the empty string
 |           Or (recursive case):
   ↰         Call this predicate recursively on
  k          The input with its last character removed
    ;?tᵗ     Put the result in a list with the original input's last character
        p    Try both permutations of that list
         c   Concatenate the elements into a single string
             which must match the output parameter
Source Link
DLosc
  • 39.2k
  • 5
  • 83
  • 141

Brachylog, 12 bytes

Ẹ|k↰;?tᵗ↺↙Ḋc

A predicate that takes the list of characters \$\sigma\$ as its input parameter and the string \$S\$ as its output parameter, succeeding if \$S\$ is \$\sigma\$-writable and failing if it is not. (Both \$\sigma\$ and \$S\$ are taken as Brachylog strings.) Try it online!

Explanation

The predicate is recursive:

Ẹ|k↰;?tᵗ↺↙Ḋc
Ẹ              Base case: input and output are both the empty string
 |             Or (recursive case):
   ↰           Call this predicate recursively on
  k            The input with its last character removed
    ;?tᵗ       Put the result in a list with the original input's last character
        ↺↙Ḋ   Try both orders of that list
            c  Concatenate the elements into a single string
               which must match the output parameter

The ↺↙Ḋ idiom means "rotate the list counterclockwise between 0 and 9 times," which is less efficient but one byte shorter than the more-obvious {|↔} ("the list as-is or reversed").