Anagram Generator Function

  • Share this:

Code introduction


This function takes a word as input and returns all possible permutations of that word, excluding the word itself.


Technology Stack : collections.Counter, itertools.permutations

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from collections import Counter
from itertools import permutations

def anagram_solver(word):
    """
    This function takes a word and returns all possible anagrams of that word.
    """
    if not word:
        return []

    # Generate all permutations of the word
    perms = permutations(word)
    # Convert permutations to strings and filter out duplicates
    anagrams = set(''.join(p) for p in perms if ''.join(p) != word)
    return sorted(list(anagrams))