# LS, Part 1: Ana Gram

If you have read The Hostile Hospital, you would know that the Baudelaire orphans, from one of the scraps of paper recovered from the Quagmire's notebooks, they discover a name, "Ana Gram". Later on, they realize that means the word "anagram", not a name. And that information helps them in finding out who Count Olaf disguised Violet Baudelaire as (Laura V. Bleediotie). They have to search through the entire hospital list, but trace the correct person down. However, what if they lived in a time where YOU helped them? Not personally, but with a program?

So your task today is simple. Given a string a and an array, return a smaller array that contains all the possible anagrams of a.

## Rules:

• This is , so shortest answer wins
• Make sure to lowercase everything in the array and the string
• Two strings that are the same are not anagrams after normalizing
• Make sure to watch out for initials! You should also remove them
• There may be multiple possible anagrams. Output them all

## Test cases:

String, Array
-> Array

"Violet", ["Veilo","Efh", "Telvio","veliot"]
-> ["Telvio","veliot"]

"Krotasmon", ["Krotas Mon","Krota","Monsakrot", "Trokasmont","KROTASMON"]
-> ["Monsakrot"]

"Laura V. Bleediotie", ["Violet Baudelaire", "Violet. Baudelaire", "VIOLeT BAUDELAIRE", "BUADELIAR VIOLETM", "Laura V. Bleediotie"]
-> ["Violet Baudelaire", "Violet. Baudelaire", "VIOLeT BAUDELAIRE"]


• Closely related Nov 18, 2022 at 7:45
• Yes, I knew that and Manage Trash So. Nov 18, 2022 at 8:25
• Two strings that are the same are not anagrams -> Can you confirm that the comparison should be done after normalization? (same case and non-letter characters removed). For instance, my understanding is that Krotas Mon is not an anagram of Krotasmon. Nov 18, 2022 at 14:28
• Yes, it has to be done after normalisation. Nov 18, 2022 at 14:41
• Please also make that clear in the question itself. Nov 18, 2022 at 18:55

# Python, 106 bytes

-12 by mousetail, Kevin Cruijssen
-5 by Jitse

lambda w,l,S=sorted:[x for x in l if S(g(x))==S(g(w))!=g(w)!=g(x)]
g=lambda s:s.lower().translate(['']*96)


Attempt This Online!

• " |\." can be \W for -2 bytes. Nov 18, 2022 at 10:00
• 119 bytes Nov 18, 2022 at 10:01
• @mousetail You can remove two more spaces at s.lower()if'' Nov 18, 2022 at 10:03
• Also you are allowed to not count the f= for -2 bytes, if you move that line to the top Nov 18, 2022 at 10:04
• 106 Nov 18, 2022 at 13:09

# 05AB1E, 14 bytes

šl€áD€{ćQsćÊ*Ï


Inputs in the order String, Array.

Explanation:

š         # Prepend the first (implicit) input to the second (implicit) input-list
l        # Convert everything in the list to lowercase
€á      # Only keep the letters of each string in the list
D     # Duplicate this list
€{   # Sort the characters of each string in the list
ć  # Extract head; push the modified list and modified first input separated to
# the stack
Q # Check which modified strings in the list are equal to this modified input
s     # Swap so the duplicated list is at the top again
ć    # Extract its head as well
Ê   # Check which partially-modified strings in the list are NOT equal to this
# partially-modified input
*     # Combine the checks at the same positions (vectorized multiply that acts as a
# vectorized logical AND)
Ï    # Only keep the strings from the second (implicit) input-list at the truthy
# positions
# (after which the filtered list is output implicitly as result)

• Not yet updated in the question, but Commented that e.g. Krotas Mon is not an anagram of Krotasmon (the equality check is post-filtering and lowering). Nov 18, 2022 at 20:26
• @JonathanAllan Thanks for the heads up. Should be fixed (at the cost of no bytes). Nov 18, 2022 at 23:14

# Jelly, 18 bytes

ŒlfØa,Ṣ$Ç⁼"Ç}</ðƇ  A dyadic Link that accepts the list of potentials on the left and the identity on the right and yields the filtered list. Try it online! ### How? ŒlfØa,Ṣ$ - Helper Link: list of characters, Name
Œl       - lower case (Name)
Øa    - "a-z"
f      - (lowered Name) filter keep ("a-z")
$- last two links as a monad - f(X=that): Ṣ - sort (X) , - (X) pair with (that) Ç⁼"Ç}</ðƇ - Main Link: Potentials; Identity Ƈ - keep Potentials, P, for which: ð - dyadic chain - f(P, Identity): Ç - call Helper with Name=P Ç} - call Helper with Name=Identity " - zip with: ⁼ - equal? / - reduce by: < - less than? i.e. alphas(lowered(P)) == alphas(lowered(Identity)) less than? sorted(alphas(lowered(P))) == sorted(alphas(lowered(Identity)))  # Charcoal, 37 bytes ≔Φ↧Ｓ№βιθＷＳ«≔Φ↧ι№βκη¿‹⁼ηθ⬤⁺ηθ⁼№ηκ№θκ⟦ι  Try it online! Link is to verbose version of code. Takes the array as a list of newline-terminated strings. Explanation: ≔Φ↧Ｓ№βιθ  Lower case the target anagram and only keep letters. ＷＳ«  Loop through the array. ≔Φ↧ι№βκη  Lower case the current element and only keep letters. ¿‹⁼ηθ⬤⁺ηθ⁼№ηκ№θκ If the strings do not equal and both strings contain equal counts of all the letters in both strings: ⟦ι  Output the current element on its own line. • Not yet updated in the question, but Commented that e.g. Krotas Mon is not an anagram of Krotasmon (the equality check is post-filtering and lowering). Nov 18, 2022 at 20:27 • @JonathanAllan Thanks, updated. – Neil Nov 18, 2022 at 21:00 # C (clang), 163155151134218193186 169 bytes -3 bytes thanks to ceilingcat!! +84 bytes thanks to c-- -24 bytes thanks to c-- u[256];v[256];*w;q(*h,*j){bzero(h,512);for(w=h+128;*j;j++)*j>64&&h[*w++=32|*j]++;}f(**o,i){for(q(u,i);*o;o++)q(v,*o),bcmp(u,v,w-v<<2)*!bcmp(u,v,512)&&printf("%ls,",*o);}  Try it online! • +1 for "+84 bytes thanks to c--", it's probably not a good approach though – c-- Nov 20, 2022 at 18:22 # Nibbles, 12 bytes (24 nibbles) |@~^+%;~$.|$$a(_@;  | # filter @ # the second argment (=array of possible anagrams) ~ # returning elements that are falsy for: ;~ # apply and save the following function to each element: | # filter each element$a # to keep only alphabetic characters . # and then map over each character ($         #       converting to lowercase
$_ # apply the saved function to the first argument % # split each normalized element # by the normalized first argment + # and flatten # (this removes normalized identities) ^ # and then get the element-wise xor @;$    #     with the normalized first argument
#     (so anagrams give empty list,
#     identities/non-anagrams give lists of all/different characters)


# Ruby, 80 bytes

->s,a{g=->s{s.upcase.gsub(/[^A-Z]/,"").chars.sort}
a.select{s!=_1&&g[s]==g[_1]}}


Attempt This Online!

• Not yet updated in the question, but Commented that e.g. Krotas Mon is not an anagram of Krotasmon (the equality check is post-filtering and lowering). Nov 18, 2022 at 20:27

# JavaScript (ES10*), 105 bytes

* Not a strict requirement, but starting with ECMAScript 2019 (aka ES10), the .sort() method is guaranteed to be stable. This used to be left to the implementation.

Expects (string)(array).

s=>a=>a.filter(w=>(g=c=>(h=s=>[...s.toLowerCase().replace(/[^a-z]/g,'')].sort(c)+0)(w)==h(s))()&!g(_=>1))


Try it online!

### Commented

For each test, the helper function g is called twice: once to check that the normalized strings are equal when the letters are sorted, and once to check that they're different when no sort is applied.

s =>                    // s   = reference string
a =>                    // a[] = array of strings
a.filter(w =>           // for each string w in a[]:
( g = c =>            //   g is a helper function taking a callback c
( h = s =>          //     h is a helper function taking a string s
[...              //       split into an array of characters:
s.toLowerCase() //         convert s to lower case
.replace(       //         remove ...
/[^a-z]/g, '' //         ... all non-letter characters
)               //
].sort(c)         //       sort the resulting array using the callback c
+ 0               //       add a final 0 to coerce to a string
)(w) == h(s)        //     is the normalization of w equal to that of s?
)()                   //   we first call g() with an undefined callback
&                     //   so that the default lexicographical sort is used
!g(_ => 1)            //   we then call g() with a callback that leaves the
//   arrays unchanged
)                       // end of filter()
`