This question is heavily based off this question, but should pose a number of additional difficulties.

Your Task

You must write a program or function that, when receiving a string, prints out all possible anagrams of it. For the purpose of this question, an anagram is a string that contains the same character as the original string, but is not the original string. A subanagram is an anagram of a substring of an inputted string. Anagrams and subanagrams do not have to be or contain actual words.


You may accept a string, which may be of any length > 0, by any standard input method. It may contain any ASCII characters.


You may output all of the possible anagrams and subanagrams of the inputted string in any standard way. You must not output the same string twice, or output a string equal to the input.

Other Rules

Standard Loopholes are disallowed


This is , least bytes wins.

  • \$\begingroup\$ Is the empty string a possible anagram? \$\endgroup\$ Commented Jun 12, 2017 at 1:20
  • \$\begingroup\$ Is outputting the original string/sustrings allowed? \$\endgroup\$ Commented Jun 12, 2017 at 2:25
  • \$\begingroup\$ @CalculatorFeline "You must not output the same string twice, or output a string equal to the input." \$\endgroup\$ Commented Jun 12, 2017 at 2:30
  • \$\begingroup\$ @DigitalTrauma, "You may accept a string, which may be of any length > 0, by any standard input method". (emphasis added) \$\endgroup\$
    – Gryphon
    Commented Jun 12, 2017 at 10:41
  • 4
    \$\begingroup\$ Some Test cases would be helpful \$\endgroup\$
    – Mr. Xcoder
    Commented Jun 12, 2017 at 12:05

9 Answers 9


05AB1E, 7 bytes


A function that accepts a string from input and leaves a list of strings on the stack. As a full program a representation of the list is printed.

Try it online!


        - push input
Π      - all substrings
 €œ     - for €ach: all permutations
   ˜    - flatten
    Ù   - de-duplicate
     ¹  - push 1st input onto top of stack
      K - pop a,b; push a without any b's (remove the copy of the input from the list)
        - as a full program: implicit print of the top of the stack
  • \$\begingroup\$ And... you managed something even shorter. \$\endgroup\$
    – Gryphon
    Commented Jun 12, 2017 at 1:11
  • \$\begingroup\$ It's the same algorithm, just less bytes. \$\endgroup\$ Commented Jun 12, 2017 at 1:17
  • \$\begingroup\$ Yes, the language change was all, but its still impressive. \$\endgroup\$
    – Gryphon
    Commented Jun 12, 2017 at 1:18
  • \$\begingroup\$ @ais523 Looks like I got both the wrong way around! \$\endgroup\$ Commented Jun 12, 2017 at 1:42
  • \$\begingroup\$ @ais523 I think that it is fixed. \$\endgroup\$ Commented Jun 12, 2017 at 2:03

Brachylog (2), 7 bytes


Try it online!


{  }ᶠ    Find all
  p        permutations of
 s         a substring of {the input}
     d   Remove duplicates (leaving the list otherwise in the same order)
      b  Remove the first (the input itself)
  • \$\begingroup\$ What does the (2) mean? \$\endgroup\$
    – Gryphon
    Commented Jun 12, 2017 at 10:35
  • \$\begingroup\$ @Gryphon (afaik) there are 2 versions of branchylog, this is using V2. \$\endgroup\$ Commented Jun 12, 2017 at 11:24
  • 1
    \$\begingroup\$ Ok, wasn't sure if it was version number or a possible byte count using a different, and possibly illegal method. \$\endgroup\$
    – Gryphon
    Commented Jun 12, 2017 at 12:39
  • 1
    \$\begingroup\$ That's the second time I've been asked now. I guess I'll have to start writing it as (v2). \$\endgroup\$
    – user62131
    Commented Jun 12, 2017 at 20:56

Jelly, 9 bytes


A monadic link accepting a list and returning a list of all distinct sub-anagrams except the input itself.

Try it online! (the footer pretty-prints the resulting list by joining with newlines.)


ẆŒ!€;/QḟW - Link: list of characters, s
Ẇ         - all contiguous sublists of s
 Œ!€      - all permutations for €ach sublist now a list of lists of lists)
     /    - reduce by:
    ;     -   concatenation (flattens the list by one level)
      Q   - de-duplicate (gets the unique entries)
        W - wrap s in a list (otherwise filtering will attempt to remove characters)
       ḟ  - filter discard from left if in right (remove the one equal to the input)

Pyth, 12


Online test.

         Q       # input
       .: )      # all substrings
    .pM          # all permutations of all substrings
  .n             # flatten
 {               # deduplicate
-          ]Q    # subtract (list of) (implicit) input
  • \$\begingroup\$ @ais523 Redone - I think it is correct now. \$\endgroup\$ Commented Jun 12, 2017 at 17:35

Japt, 10 bytes

à má c â Å

Try it online!

I got to use à, á, and â all in one answer, in order too. What a coincidence...


 à má c â Å
Uà má c â s1  // Ungolfed
              // Implicit: U = input string
Uà            // Take all combinations of characters in the input string.
   má         // Map each combination to all of its permutations.
      c       // Flatten into a single array.
        â     // Uniquify; remove all duplicates.
          s1  // Remove the first item (the input) from the resulting array.
              // Implicit: output resulting array, separated by commas
  • 1
    \$\begingroup\$ You even managed Å too. \$\endgroup\$
    – Gryphon
    Commented Jun 12, 2017 at 1:12

Mathematica, 60 bytes


Permutations takes an optional numerical argument which tells it how many of the input values to use for the permutations. If we give it the length of the input, it will generate the permutations for all subsets of the input without duplicates. All we need to do is remove the input.


Java 8, 313 312 306 297 bytes

import java.util.*;s->{Set l=new HashSet();for(int z=s.length(),i=0,j;i<z;i++)for(j=i;j<z;)p("",s.substring(i,++j),l);l.remove(s);l.forEach(System.out::println);}void p(String p,String s,Set l){int n=s.length(),i=0;if(n<1)l.add(p);for(;i<n;p(p+s.charAt(i),s.substring(0,i)+s.substring(++i,n),l));}

Modified version of my answer here, where p("",s,l); has been replaced with for(int z=s.length(),i=0,j;i<z;i++)for(j=i;j<z;p("",s.substring(i,j+++1),l));

-6 bytes thanks to @OlivierGrégoire in my linked answer
-9 bytes thanks to @ceilingcat

Try it online.

Explanation of this part:

for(int z=s.length(),i=0,j;i<z;i++) // Loop `i` in the range [0,length):
  for(j=i;j<z;)                     //  Inner loop `j` in the range [i,length):
    p("",                           //   Call the permutation-method,
      s.substring(i,++j),l));       //   with a substring at index-range [i,j]

Vyxal, 9 bytes



<implicit input>
ǎ                 - Get substrings
 ƛṖ;              - Map over substrings: Get permutations
    f             - Flatten it
     U            - Uniquify, remove all duplicates
      '⁰≠         - Filter lambda: Keep all elements that don't equal input
<implicit output>

Try it Online!


Perl 6, 75 bytes

{unique(flat $_,.comb.combinations.skip».permutations.map(*».join)).skip}

Try it


{                    # bare block lambda with implicit parameter 「$_」

  unique(            # return each (sub)anagram once

    flat             # unstructure the following (List of Lists into flat List)
      $_,            # the input (so we can skip it at the end)

      .comb          # split the input into graphemes
      .combinations  # get all the combinations
      .skip\         # skip the first empty combination
      ».permutations # get all the permutations of each combination
      .map(*».join)  # join the inner permutations

  ).skip             # skip the first value (the input)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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