-4

An anagram is a word formed from another by rearranging its letters, using all the original letters exactly once; for example, orchestra can be rearranged into carthorse.

I want to write a function to return all anagrams of a given word (including the word itself) in any order.

For example GetAllAnagrams("abba") should return a collection containing "aabb", "abab", "abba", "baab", "baba", "bbaa".

Any help would be appreciated.

Farshid
  • 5,134
  • 9
  • 59
  • 87
  • Use Nuget package Combinatorics: https://www.nuget.org/packages/Combinatorics/ - but those aren't words, you'd need a dictionary too if you want actual "anagrams" – Ian Mercer Jul 08 '16 at 05:40
  • 3
    Show us your code. – Lennart Jul 08 '16 at 05:43
  • Persuant to @IanMercer's comment, your notation is a bit off. A 'word' is typically not just any combination of letters, but specifically only the combinations that have meaning in a particular language (or any language). A much better way to ask the question is by 'listing the set of all unique strings that can be made using the input characters', which the solution I provided below does. – Ehryk Jul 08 '16 at 06:35

2 Answers2

1

Here is a working function, making use of a GetPermutations() extension found elsewhere on stack overflow

public static List<string> GetAnagrams(string word)
{
    HashSet<string> anagrams = new HashSet<string>();
    char[] characters = word.ToCharArray();

    foreach (IEnumerable<char> permutation in characters.GetPermutations())
    {
        anagrams.Add(new String(permutation.ToArray()));
    }

    return anagrams.OrderBy(x => x).ToList();
}

Here is the GetPermutations() extension and it's other necessary extensions:

    public static IEnumerable<IEnumerable<T>> GetPermutations<T>(this IEnumerable<T> enumerable)
    {
        var array = enumerable as T[] ?? enumerable.ToArray();

        var factorials = Enumerable.Range(0, array.Length + 1)
            .Select(Factorial)
            .ToArray();

        for (var i = 0L; i < factorials[array.Length]; i++)
        {
            var sequence = GenerateSequence(i, array.Length - 1, factorials);

            yield return GeneratePermutation(array, sequence);
        }
    }

    private static IEnumerable<T> GeneratePermutation<T>(T[] array, IReadOnlyList<int> sequence)
    {
        var clone = (T[])array.Clone();

        for (int i = 0; i < clone.Length - 1; i++)
        {
            Swap(ref clone[i], ref clone[i + sequence[i]]);
        }

        return clone;
    }

    private static int[] GenerateSequence(long number, int size, IReadOnlyList<long> factorials)
    {
        var sequence = new int[size];

        for (var j = 0; j < sequence.Length; j++)
        {
            var facto = factorials[sequence.Length - j];

            sequence[j] = (int)(number / facto);
            number = (int)(number % facto);
        }

        return sequence;
    }

    static void Swap<T>(ref T a, ref T b)
    {
        T temp = a;
        a = b;
        b = temp;
    }

    private static long Factorial(int n)
    {
        long result = n;

        for (int i = 1; i < n; i++)
        {
            result = result * i;
        }

        return result;
    }
}

Here is a screenshot of the result:

GetAnagrams - abba

And, finally, a github repository of the complete Visual Studio solution: Github

Community
  • 1
  • 1
Ehryk
  • 1,930
  • 2
  • 27
  • 47
-1
import java.util.Scanner;

import java.lang.String;

public class KrishaAnagram

{
 public static void main(String[] args) {
  Scanner Scan = new Scanner(System.in);
  String s1, s2;
  int sum1, sum2;

  sum1 = sum2 = 0;

  System.out.print("Enter fisrt string: ");
  s1 = Scan.next();
  System.out.print("Enter Second string: ");
  s2 = Scan.next();

  if (s1.length() != s2.length()) {
   System.out.println("NOT ANAGRAM");
  } else {
   for (int i = 0; i < s1.length(); i++) {
    char ch1 = s1.charAt(i);
    char ch2 = s2.charAt(i);
    sum1 += (int) ch1;
    sum2 += (int) ch2;
   }
   if (sum1 == sum2)
    System.out.println("IT IS AN ANAGRAM : s1 = " + sum1 + "s1 = " + sum2);
   else
    System.out.println("IT IS NOT AN ANAGRAM : s1 = " + sum1 + "s1 = " + sum2);;
  }
 }
}

//This is My way of solving anagram in java,Hope it helps.