Find all the Anagrams, and the Subanagrams too!

13

1

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.

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.

Gryphon

Posted 2017-06-12T00:39:01.347

Reputation: 6 697

Is the empty string a possible anagram? – Digital Trauma – 2017-06-12T01:20:42.120

Is outputting the original string/sustrings allowed? – CalculatorFeline – 2017-06-12T02:25:46.083

@CalculatorFeline "You must not output the same string twice, or output a string equal to the input." – Jonathan Allan – 2017-06-12T02:30:29.913

@DigitalTrauma, "You may accept a string, which may be of any length > 0, by any standard input method". (emphasis added) – Gryphon – 2017-06-12T10:41:26.067

4Some Test cases would be helpful – Mr. Xcoder – 2017-06-12T12:05:18.570

"an anagram is a string that contains the same character as the original string, but is not the original string": it's unclear to me whether it applies to subanagrams as well (in which case AB is not a valid subanagram of ABCD, but BA is). Could you please clarify? – Arnauld – 2017-06-12T12:20:06.360

Should tt be an anagram of test? If yes, then quite some given solutions are not working. If not, then I guess that aa shouldn't be an anagram of aabb and again some solutions are not working. Some examples to explain this would be helpful :) – Michiel uit het Broek – 2017-06-12T14:20:45.933

No, tt is not an anagram or a subanagram of "test". However, I never specifically said you couldn't output additional strings, which was a mistake in the question. I just said you couldn't output the same string twice, which I don't think any answer is doing, or output the original string, which I also don't think any answer is doing. I would change the question to not allow substrings of the input, but it's too late now, as that would invalidate existing answers. – Gryphon – 2017-06-12T16:49:17.110

Answers

8

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

Jonathan Allan

Posted 2017-06-12T00:39:01.347

Reputation: 67 804

And... you managed something even shorter. – Gryphon – 2017-06-12T01:11:29.377

It's the same algorithm, just less bytes. – Jonathan Allan – 2017-06-12T01:17:40.393

Yes, the language change was all, but its still impressive. – Gryphon – 2017-06-12T01:18:40.453

@ais523 Looks like I got both the wrong way around! – Jonathan Allan – 2017-06-12T01:42:28.620

@ais523 I think that it is fixed. – Jonathan Allan – 2017-06-12T02:03:59.237

9

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)

user62131

Posted 2017-06-12T00:39:01.347

Reputation:

What does the (2) mean? – Gryphon – 2017-06-12T10:35:04.737

@Gryphon (afaik) there are 2 versions of branchylog, this is using V2. – John Hamilton – 2017-06-12T11:24:33.490

1Ok, wasn't sure if it was version number or a possible byte count using a different, and possibly illegal method. – Gryphon – 2017-06-12T12:39:23.810

1That's the second time I've been asked now. I guess I'll have to start writing it as (v2). – None – 2017-06-12T20:56:18.447

7

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)

Jonathan Allan

Posted 2017-06-12T00:39:01.347

Reputation: 67 804

4

Pyth, 12

-{.n.pM.:Q)]

Online test.

         Q       # input
       .: )      # all substrings
    .pM          # all permutations of all substrings
  .n             # flatten
 {               # deduplicate
-          ]Q    # subtract (list of) (implicit) input

Digital Trauma

Posted 2017-06-12T00:39:01.347

Reputation: 64 644

@ais523 Redone - I think it is correct now. – Digital Trauma – 2017-06-12T17:35:45.523

3

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

ETHproductions

Posted 2017-06-12T00:39:01.347

Reputation: 47 880

1You even managed Å too. – Gryphon – 2017-06-12T01:12:59.603

1

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.

Martin Ender

Posted 2017-06-12T00:39:01.347

Reputation: 184 808

1

Java 8, 313 312 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));

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

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)

Kevin Cruijssen

Posted 2017-06-12T00:39:01.347

Reputation: 67 575

0

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

Brad Gilbert b2gills

Posted 2017-06-12T00:39:01.347

Reputation: 12 713