Skip to main content
saved 1 byte
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

JavaScript (ES6),  83 82  8281 bytes

Returns 0 if the input string is a Boolean valuesuperpermutation, or 1 if it's not.

f=(s,a=[...new Set(s)],p)=>s=>!s.match(p)&&a|a.everysome((c,n)=>f(s,a.filter(_=>n--),[p]+c))

Try it online!Try it online!

f = (                     // f is a recursive function taking:
  s,                      //   s = input string
  a = [...new Set(s)],    //   a[] = list of unique characters in s
  p                       //   p = current permutation, initially undefined
) =>                      //
  !s.match(p) &&|           // abortforce the result to 1 if p is not found in s
                          // NB: s.match(undefined) is truthy because it's equivalent
                          //     to looking for an empty string in s
  a.everysome((c, n) =>        // otherwise, for each character c at position n in a[]:
    f(                    //   do a recursive call:
      s,                  //     pass s unchanged
      a.filter(_ => n--), //     remove the n-th character in a[] (0-indexed)
      [p] + c             //     coerce p to a string and append c to p
    )                     //   end of recursive call
  )                       // end of everysome()

JavaScript (ES6),  83  82 bytes

Returns a Boolean value.

f=(s,a=[...new Set(s)],p)=>s.match(p)&&a.every((c,n)=>f(s,a.filter(_=>n--),[p]+c))

Try it online!

f = (                     // f is a recursive function taking:
  s,                      //   s = input string
  a = [...new Set(s)],    //   a[] = list of unique characters in s
  p                       //   p = current permutation, initially undefined
) =>                      //
  s.match(p) &&           // abort if p is not found in s
                          // NB: s.match(undefined) is truthy because it's equivalent
                          //     to looking for an empty string in s
  a.every((c, n) =>       // otherwise, for each character c at position n in a[]:
    f(                    //   do a recursive call:
      s,                  //     pass s unchanged
      a.filter(_ => n--), //     remove the n-th character in a[] (0-indexed)
      [p] + c             //     coerce p to a string and append c to p
    )                     //   end of recursive call
  )                       // end of every()

JavaScript (ES6),  83 82  81 bytes

Returns 0 if the input string is a superpermutation, or 1 if it's not.

f=(s,a=[...new Set(s)],p)=>!s.match(p)|a.some((c,n)=>f(s,a.filter(_=>n--),[p]+c))

Try it online!

f = (                     // f is a recursive function taking:
  s,                      //   s = input string
  a = [...new Set(s)],    //   a[] = list of unique characters in s
  p                       //   p = current permutation, initially undefined
) =>                      //
  !s.match(p) |           // force the result to 1 if p is not found in s
                          // NB: s.match(undefined) is truthy because it's equivalent
                          //     to looking for an empty string in s
  a.some((c, n) =>        // for each character c at position n in a[]:
    f(                    //   do a recursive call:
      s,                  //     pass s unchanged
      a.filter(_ => n--), //     remove the n-th character in a[] (0-indexed)
      [p] + c             //     coerce p to a string and append c to p
    )                     //   end of recursive call
  )                       // end of some()
minor update
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

That's why we can use a recursive function that recursively builds each permutation \$p\$ of the symbols and tests whether \$p\$ exists in \$s\$ at each iteration, even when \$p\$ is still incomplete.

That's why we can use a recursive function that recursively builds each permutation \$p\$ of the symbols and tests whether \$p\$ exists in \$s\$ at each iteration, even when \$p\$ is still incomplete.

That's why we can use a function that recursively builds each permutation \$p\$ of the symbols and tests whether \$p\$ exists in \$s\$ at each iteration, even when \$p\$ is still incomplete.

added an explanation
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

How?

If all permutations of the \$N\$ symbols are present in the input string \$s\$, so are all prefixes of said permutations. Therefore, it's safe to test that all \$p\$ are found in \$s\$ even when \$p\$ is an incomplete permutation whose size is less than \$N\$.

That's why we can use a recursive function that recursively builds each permutation \$p\$ of the symbols and tests whether \$p\$ exists in \$s\$ at each iteration, even when \$p\$ is still incomplete.

Commented

Commented

How?

If all permutations of the \$N\$ symbols are present in the input string \$s\$, so are all prefixes of said permutations. Therefore, it's safe to test that all \$p\$ are found in \$s\$ even when \$p\$ is an incomplete permutation whose size is less than \$N\$.

That's why we can use a recursive function that recursively builds each permutation \$p\$ of the symbols and tests whether \$p\$ exists in \$s\$ at each iteration, even when \$p\$ is still incomplete.

Commented

minor update
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
minor clarification
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
added a commented version
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
Post Undeleted by Arnauld
fixed version and saved 1 byte
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
Post Deleted by Arnauld
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading