# Find all the Anagrams, and the Subanagrams too!

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

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.

## Input

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

## Output

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

## Scoring

This is , least bytes wins.

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

# 05AB1E, 7 bytes

Œ€œ˜Ù¹K

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!

### How?

- 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
• And... you managed something even shorter. – Gryphon Jun 12 '17 at 1:11
• It's the same algorithm, just less bytes. – Jonathan Allan Jun 12 '17 at 1:17
• Yes, the language change was all, but its still impressive. – Gryphon Jun 12 '17 at 1:18
• @ais523 Looks like I got both the wrong way around! – Jonathan Allan Jun 12 '17 at 1:42
• @ais523 I think that it is fixed. – Jonathan Allan Jun 12 '17 at 2:03

# Brachylog (2), 7 bytes

{sp}ᶠdb

Try it online!

## Explanation

{sp}ᶠdb
{  }ᶠ    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)
• What does the (2) mean? – Gryphon Jun 12 '17 at 10:35
• @Gryphon (afaik) there are 2 versions of branchylog, this is using V2. – John Hamilton Jun 12 '17 at 11:24
• Ok, wasn't sure if it was version number or a possible byte count using a different, and possibly illegal method. – Gryphon Jun 12 '17 at 12:39
• That's the second time I've been asked now. I guess I'll have to start writing it as (v2). – user62131 Jun 12 '17 at 20:56

# Jelly, 9 bytes

ẆŒ!€;/QḟW

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.)

### How?

ẆŒ!€;/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

-{.n.pM.:Q)]
Q       # input
.: )      # all substrings
.pM          # all permutations of all substrings
.n             # flatten
{               # deduplicate
-          ]Q    # subtract (list of) (implicit) input
• @ais523 Redone - I think it is correct now. – Digital Trauma Jun 12 '17 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...

### Explanation

à 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
• You even managed Å too. – Gryphon Jun 12 '17 at 1:12

## Mathematica, 60 bytes

DeleteCases[""<>#&/@Permutations[c=Characters@#,Tr[1^c]],#]&

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, 313312 306 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+++1),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);else for(;i<n;p(p+s.charAt(i),s.substring(0,i)+s.substring(i+++1,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));

Explanation of this part:

Try it here.

for(int l=s.length(),i=0,j;i<l;i++)
// Loop (1) from 0 to the length of the String (exclusive)
for(j=i+1;j<=l;              //  Loop (2) from 1 to the length of the String (exclusive)
p("",                      //   Call the permutation-method,
s.substring(i,j+++1),l));  //   with a substring from i to j (inclusive)
//  End of loop (2) (implicit / single-line body)
// End of loop (1) (implicit / single-line body)

# Perl 6, 75 bytes

{unique(flat $_,.comb.combinations.skip».permutations.map(*».join)).skip} Try it ## Expanded: { # 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)
}