[Question]
  [
A [superpermutation](https://en.wikipedia.org/wiki/Superpermutation) on n symbols is a string which contains every permutation of n symbols in its body. For instance, `123121321` is a superpermutation on three symbols because it contains `123`, `132`, `213`, `231`, `312` and `321` as substrings.
# The Challenge
Given a string composed of n unique symbols (and, optionally, n), output whether it is a superpermutation on n symbols.
# Rules
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so the shortest answer in bytes wins.
* Assume only valid input will be given.
* Assume n is greater than 0
* Input and output can assume whatever form is most convenient, e.g. the series of symbols can be a string, a list, an integer, a set of n bitmasks, etc, so long as it is indicated in the answer. Additionally, anything may be used as a symbol provided it is distinct from all the other symbols.
# Test Cases
```
In: 1234
Out: False
In: 1
Out: True
In: 11
Out: True
In: 123121321
Out: True
In: 12312131
Out: False
```
See also: [this question](https://codegolf.stackexchange.com/questions/179607/) about generating superpermutations
  
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 4 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
ÙœåP
```
Only takes input \$J\$ (I don't need \$n\$ with this approach).
[Try it online](https://tio.run/##yy9OTMpM/f//8Myjkw8vDfj/39DI2NDI0NjIEAA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfaXL/8Mzj04@vDTgv87/aEMjYxMdQx1DIDIyNjQyNDaCswxjAQ).
**Explanation:**
```
Ù     # Uniquify the digits of (implicit) input-integer
 œ    # Get all permutations of this uniquified integer
  å   # Check for each if it's a substring of the (implicit) input-integer
   P  # And check if this is truthy for all of them
      # (after which the result is output implicitly)
```
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 20 [bytes](https://codegolf.meta.stackexchange.com/questions/9428/when-can-apl-characters-be-counted-as-1-byte-each/)
```
{(!⍺)=+/⍺=⍴∘∪¨∪⍺,/⍵}
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///v1pD8VHvLk1bbX0gZfuod8ujjhmPOlYdWgEkgCI6QOGttf/THrVNeNTb96ir@VHvGqCiQ@uNH7VNfNQ3NTjIGUiGeHgG/zdRSFMAsoEq1Q2NjE3UuQzhAoZAxeqGCshC6oaG6lzGKFoMjQyNjbCKGqoDAA "APL (Dyalog Unicode) – Try It Online")
Takes `n` on the left and `J` on the right
### How?
```
⍺,/⍵   ⍝ Overlapping sublists of length n in J
∪      ⍝ Unique sublists
⍴∘∪¨   ⍝ Length of the unique elements of each unique sublist
+/⍺=   ⍝ How many are equal to n?
(!⍺)=  ⍝ Is this equal to the number of permutations of n symbols?
```
[Answer]
# [Python 3](https://docs.python.org/3/), 79 bytes
```
lambda s:all(''.join(p)in s for p in permutations({*s}))
from itertools import*
```
[Try it online!](https://tio.run/##TYwxDsIwEAT7vOKa6JwoIGxTReInNEbEwsj2Wb5LgRBvNwkVxa5mit3ykgdl2/zl2qJLt7sDnl2MCvH4pJBVGUIGBk8VCmxYlppWcRIos3qP/BmGzldKEGSpQhQZQipUZWz7ZrOM2tgzTqj3/MpYbbQ1f6xx7qDUkEVhf9Anxp4nr3h7b18 "Python 3 – Try It Online")
---
# [Python 2](https://docs.python.org/2/), 81 bytes
```
lambda s:all(''.join(p)in s for p in permutations(set(s)))
from itertools import*
```
[Try it online!](https://tio.run/##TYyxDgIhEER7vmKby4I5jYDVJf6JDcYjYoAl7Fr49XhnZTGT94qZ9pEnVTfi9TZyKPdHAF5Czhrx9KJUdTOpAkOkDg02bGsvbwmSqLLmVTQbY1TsVCDJ2oUoM6TSqMth7KvNKlrnLzij3fMr562z3v2xxUVB66kK4HS0Z8aJ57jdq/EF "Python 2 – Try It Online")
[Answer]
# 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!](https://tio.run/##fcuxDsIgFEDR3a/QTu9FeAngSn/CsWkMQdAaCqQQXfx3tKtpXO/JfZinKXaZcuUxXV1rXkNhRg9EFN1rf3YVCo4so@4PhWZT7R0yvg2VNDsAy@JX/PqQn0J1C1x0HzlHNuTxaBGbTbGk4CikG3johFSnDnH3m7faZpRKSKHkH1upfQA "JavaScript (Node.js) – Try It Online")
### 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 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
```
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()
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt) v2.0a0, ~~10~~ 8 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
Saved 2 bytes with the clarification that the string can only contain the digits in `[1,n]`.
```
â á e!øU
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=2.0a0&code=4iDhIGUh%2bFU&input=IjEyMzEyMTMyMSI)
```
â á e!øU     :Implicit input of string U
â            :Deduplicate
  á          :Permutations
    e        :All
     !øU     :  Contained in U
```
[Answer]
# [Io](http://iolanguage.org/), 104 bytes
```
method(x,n,K :=Range 1 to(n)asList;x map(i,v,x slice(i,i+n))unique select(x,x sort==K)size==K reduce(*))
```
[Try it online!](https://tio.run/##jYyxDoJAEER7v2LLW93mgErDF2BlbUNg0U2OO@AOQ/z58wKxMMbEal5mXkZc7OBYwjX2HO6uVQtZqlJzqe2NQUNwymLtz@LDaYG@HpTQgxbwRhpOLAeLOFsZZwbPhpuQLtLsplCWFXp5ckqYuJ2Tv0eMnTLpTGmCjCAnKJAKhGESG4zdvVck/V0S/Oq3qw30ytkq5//JH2Z8AQ "Io – Try It Online")
## Explanation (Ungolfed)
```
method(x,n,                        // Take the string and the num of uniquified integers
    K := Range 1 to(n)asList       // K = [1..n]
    x map(i,v,x slice(i,i+n))      // All slices of x of length n
    unique                         // Uniquify these slices
    select(x,                      // Filter: (x : current item)
        x sort==K                  //     sort(x) == [1..n]?
    ) size                         // Number of items that satisfy this
    == K reduce(*)                 // == factorial(n)?
)
```
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 7 bytes
```
dpᶠ~sᵛ?
```
Same algorithm as [@Kevin Cruijssen](https://codegolf.stackexchange.com/a/207466/95594), so upvote that.
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r/P6Xg4bYFdcUPt862//9fydDI2NDI0NjIUOk/AA "Brachylog – Try It Online")
### How it works
```
dpᶠ~sᵛ?
d       deduplicate input
 pᶠ     find all permutations
   ~sᵛ  all of them must be substrings of
      ? the input
```
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 44 bytes
```
Union[##~Partition~1]~Count~{a__/;0!=a}<#2!&
```
[Try it online!](https://tio.run/##TYyxCoMwFEV3/8IEnCxtkm6t8sC1g1A6SZCHaJPBCOnrFMyvp2IXl3M5HLgzkhlnJDtgmqr0cnZxHeexRU@WNolCx2b5OooB@/58u@QVrncu8yI9B3Rd662jjp/qCYDrAkJj0ONAo/8ALx@je5OB/RaOZS00hIwJqa6s3HbHn1IJKZQ8imDZmn4 "Wolfram Language (Mathematica) – Try It Online")
Takes a list of characters and \$n\$ as input. Returns `False` if the string is a superpermutation, and `True` otherwise.
Verifies that the number of unique sequences of \$n\$ distinct characters is (un)equal to \$n!\$.
[Answer]
# [R](https://www.r-project.org/) + gtools, 79 bytes
```
function(x,n)all(sapply(apply(permutations(n,n),1,paste0,collapse=""),grepl,x))
```
[Try it online!](https://tio.run/##Zc29CsMgFAXgPU8R7KJwh6pzhg7plKk/D2CDDYFbvXgVkqe3KV1KCofDGT44qeL8SC6tcsoxIquGC/m05VWyy3MMXX2WMH6WXCAohyjZEeEqv/1DWYZNgAZynP0RxojoiH0nhIIpeUJYlKr7Bym0sdpoa7QAq9pDe7vc@@af7dT5NFz7@gY "R – Try It Online")
An example of some awfully-verbose names for R functions and mandatory arguments!
Generates all permutations of digits 1..n, pastes them together as strings, and checks that all are present in the input string.
An alternative **66 byte** solution using the R "combinat" library would be: `function(x,n,`[`=sapply)all(permn(n)[paste0,collapse=""][grepl,x])`,
but unfortunately this library isn't installed on TIO.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
Œ!ẇ€Ạ
```
A dyadic Link accepting \$n\$ on the left and the candidate as a list of integers on the right which yields `1` (is) or `0` (is not) as appropriate.
**[Try it online!](https://tio.run/##y0rNyan8///oJMWHu9ofNa15uGvB////jf9HG@oY6RjrgEhDEB0LAA "Jelly – Try It Online")**
### How?
```
Œ!ẇ€Ạ - Link: n, L
Œ!    - all permutations of [1..n]
   €  - for each (permutation, p):
  ẇ   -   is (p) a sublist of (L)?
    Ạ - all?
```
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 44 bytes
```
Subsequences@#~SubsetQ~Permutations@Union@#&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n277P7g0qTi1sDQ1Lzm12EG5DswtCawLSC3KLS0BqsrPK3YIzQNSDspq/wOKMvNKFPQd0h20nDMSixKTS1KLivUdqpUMjYxNlHSUDEEYTBgZGxoZGhshsQ2Vav//BwA "Wolfram Language (Mathematica) – Try It Online")
@att saved 31 bytes
[Answer]
# [Haskell](https://www.haskell.org/), 57 bytes
```
import Data.List
s p=all(`isInfixOf`p)$permutations$nub$p
```
[Try it online!](https://tio.run/##BcExCoAwDADA3Vc4dFAQoe5uLoLgA0RohBaDbRqaCP6@3t0gj4@xVkyci7YLKIwbijbS8gwxdg5lpYDfHhz3hn1Jr4JiJjH0XoZrAqSZC5IaOewwDfasPw "Haskell – Try It Online")
[Answer]
# [Husk](https://github.com/barbuz/Husk), 5 bytes
```
Λ€¹Pu
```
[Try it online!](https://tio.run/##yygtzv7//9zsR01rDu0MKP3//7@SoZGxoZGhsZGhEgA "Husk – Try It Online")
Same as the Jelly answer.
[Answer]
# Scala `-deprecation -encoding=UTF-8 -feature -unchecked -language:postfixOps -language:implicitConversions -language:higherKinds -language:existentials -language:postfixOps -Xfuture -Yno-adapted-args -Ywarn-dead-code -Ywarn-numeric-widen -Ywarn-value-discard -Ywarn-unused`, 44 bytes
```
s=>s.distinct.permutations forall s.contains
```
Pretty straightforward. Finds all the distinct symbols, generates all their permutations, and then checks if each permutation is in the input string.
[Try it online](https://scastie.scala-lang.org/T5tiM4TESvmq3Y3fyPUTkA)
# Scala, ~~56~~ 54 bytes
```
(s,>)=>(1 to>).mkString.permutations forall s.contains
```
As you can tell, the superpermutation string is ~~`|`~~ `s`(lot less readable now) and `n` is `>`. It basically just generates every permutation in the range `1 to n` and checks if each of those are in the input string.
[Try it online!](https://scastie.scala-lang.org/YqSl0SDJQHyPn2tc8s0Hqw)
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 35 bytes
```
Ｎθ⁼ΠＥθ⊕ιＬΦＥη✂ηκ⁺κθ¹∧⁼κ⌕ηι⁼θＬΦι⁼μ⌕ιλ
```
[Try it online!](https://tio.run/##XYzBDoIwEETvfEWP26QeCkdOHiQhEUPiF9TSSONSaGn9/boY1MS97M7svNGjCnpWmHPrlhQvabqZAJ7XRR@si3DySeEKfZiHpCN0agEvWOt0MJNx0QxgORfsbNw9jtBYjIRvqVGwK1pttuMhWI9pBdqewnIjjm74lJPdWJKUfJfttv@vtd/XtBPkIP9NnXNVyLKSpaxKmQ9PfAE "Charcoal – Try It Online") Link is to verbose version of code. Outputs a Charcoal boolean, i.e. `-` for a superpermutation, nothing if not. Explanation:
```
Ｎθ
```
Input `n` as a number.
```
⁼ΠＥθ⊕ι
```
`n!` must equal...
```
ＬΦＥη✂ηκ⁺κθ¹
```
... the number of truncated suffixes of the string...
```
∧⁼κ⌕ηι
```
... that have not already been seen earlier in the string, and...
```
⁼θＬΦι⁼μ⌕ιλ
```
... contain `n` distinct characters.
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 10 bytes
```
.Am}dz.p{z
```
[Try it online!](https://tio.run/##K6gsyfj/X88xtzalSq@guur/f0MjY0MjQ2MjQwA "Pyth – Try It Online")
---
Explanation:
```
.Am}dz.p{z
        {z  Deduplicate, yielding the distinct digits
      .p    Permutate
  m         Map with d as variable
   }dz      Check if d is a substring of z
.A          Verify that all elements are truthy
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 9 bytes
```
UṖƛ∑⁰$c;A
```
[Try it Online!](https://lyxal.pythonanywhere.com?flags=&code=U%E1%B9%96%C6%9B%E2%88%91%E2%81%B0%24c%3BA&inputs=%27123121321%27&header=&footer=)
A mess.
[Answer]
# Java 10, ~~291~~ ~~287~~ ~~233~~ 229 bytes
```
n->{var t="";for(var d:n.split(t))t+=t.contains(d)?"":d;return p(n,"",t);}boolean p(String n,String p,String s){int l=s.length(),i=0;var r=n.contains(p);for(;i<l;)r&=p(n,p+s.charAt(i),s.substring(0,i)+s.substring(++i));return r;}
```
-4 bytes by taking inspiration from what [*@Arnauld* mentioned in his JavaScript answer](https://codegolf.stackexchange.com/a/207483/52210):
> 
> 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.
> 
> 
> 
Takes the integer-input as String.
[Try it online.](https://tio.run/##TZHBboMwDIbvfQorhykWFJV2JzI2TTuvlx6nHVJI23RpQInpNCGenQUGXQ9x7Dj6vz/OWV7l8lx@9YWR3sO71LZdAGhLyh1koWA7lAD7qjJKWij4jpy2R7AoQqNbhLAFCzn0dvncXqUDyhkTh8rxoSgzm/jaaOKESFFOSVFZChTPS3xhLCuFU9Q4CzW3MWMxoejETKtvtHhK6jnx2AaTYHKfGGWPdOIY63wlBqjL7T@mxtGM0E9GoHvIB04d@aQ4SfdKXGPsE9/s/SjLV7HG6P4gijTi7NGJrg8Prpu90QV4khS2a6VLuATWZPbjEyT@TW0AT35JeXqTXmVWfcN8sWXpevPIYpYOawzrTbpON@u7PGUdjmoAux9P6pJUDSV1UCBj@awbsQxYNIgPf8gxCSO4NXH6rK7/BQ)
**Explanation:**
```
n->{                    // Method with String as parameter and boolean return-type
  var t="";             //  Temp String, starting empty
  for(var d:n.split(t)) //  Loop over the digits of the input:
    t+=                 //   Append to String `t`:
       t.contains(d)?   //    If `t` contains this digit already:
        ""              //     Append nothing
       :                //    Else (it doesn't contain this digit yet):
        d;              //     Append this digit
  return p(n,"",t);}    //  Call the separated recursive method to check if each
                        //  permutation of `t` is a substring of `n` and return it as
// Separated recursive method to get all permutations of String `t`, and check for each
// if it's a substring of String `n`
boolean p(String n,String p,String s){
  int l=s.length(),    //  Get the length of the input-String `s`
      i=0;             //  Set the index `i` to 0
  var r=               //  Result-boolean, starting at:
        n.contains(p); //   Check that String `n` contains part `p` as substring instead
                       //   (this doesn't necessary have to be the full permutation,
                       //    but it doesn't matter if the part is smaller)
  for(;i<l;)           //  Loop `i` in the range [0, length):
    r&=                //   Add the following to the boolean-return (bitwise-AND style):
      p(               //    Do a recursive call with:
        n,p            //     The current part,
          +s.charAt(i),//     appended with the `i`'th character as new part
          s.substring(0,i)+s.substring(++i));
                       //     And the String minus this `i`'th character as new String
                       //     (and increment `i` for the next iteration in the process)
  return r;}           //  And return the resulting boolean
```
[Answer]
# [Ruby](https://www.ruby-lang.org/) `-nl`, 44 bytes
```
p$_.chars.uniq.permutation.all?{|x|$_[x*'']}
```
[Try it online!](https://tio.run/##KypNqvz/v0AlXi85I7GoWK80L7NQryC1KLe0JLEkMz9PLzEnx766pqJGJT66QktdPbb2/39DI2MTLkMuQyAyMjY0MjQ2grMM/@UXgLQV/9fNywEA "Ruby – Try It Online")
[Answer]
# T-SQL, 186 bytes
Returns 1 for true, 0 for false.
This struggles with more than 6 unique characters
```
WITH B as(SELECT distinct substring(@,number,1)a FROM spt_values),C
as(SELECT a y FROM b UNION ALL SELECT y+a FROM B,C
WHERE y like'%'+a+'%')SELECT 1/sum(1)FROM C WHERE replace(@,y,'')=@
```
**[Try it online](https://data.stackexchange.com/stackoverflow/query/1268288/verify-a-superpermutation)** ungolfed
]