14
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ Is the empty string a possible anagram? \$\endgroup\$ Jun 12, 2017 at 1:20
  • \$\begingroup\$ Is outputting the original string/sustrings allowed? \$\endgroup\$ 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\$ 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
    Jun 12, 2017 at 10:41
  • 4
    \$\begingroup\$ Some Test cases would be helpful \$\endgroup\$
    – Mr. Xcoder
    Jun 12, 2017 at 12:05

9 Answers 9

9
\$\begingroup\$

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

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)
\$\endgroup\$
4
  • \$\begingroup\$ What does the (2) mean? \$\endgroup\$
    – Gryphon
    Jun 12, 2017 at 10:35
  • \$\begingroup\$ @Gryphon (afaik) there are 2 versions of branchylog, this is using V2. \$\endgroup\$ 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
    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
    Jun 12, 2017 at 20:56
7
\$\begingroup\$

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)
\$\endgroup\$
0
4
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ @ais523 Redone - I think it is correct now. \$\endgroup\$ Jun 12, 2017 at 17:35
3
\$\begingroup\$

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
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You even managed Å too. \$\endgroup\$
    – Gryphon
    Jun 12, 2017 at 1:12
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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]
\$\endgroup\$
0
1
\$\begingroup\$

Vyxal, 9 bytes

ǎƛṖ;fU'⁰≠

Explanation

<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!

\$\endgroup\$
0
\$\begingroup\$

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)
}
\$\endgroup\$

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.