Home / JAVA / Java Program to find Anagrams Words

Java Program to find Anagrams Words

Anagrams Program in Java:

Anagrams means different combination, the result of rearranging the letters of a word to produce new words or we can say the possible string we make by rearranging the letter of word. Anagrams of a word is to get all the permutation of a word. Here we have provided a simple anagram code in Java and there are 2 Methods to find a Anagram of a word.

Example:
Anagram of the word ‘ABC‘ are : ABC, BAC, BCA, ACB, CAB, CBA

Also you can see other solutions as we have Anagrams program in Python too for those who love to code in Python Language.

Also See:   How to Null a string or empty a string array
  • package com.jd.first;
    import java.util.*;
     
    public class AnagramsWord
     {
        public static Set<String> anagramsFinder(String str)
        {
            Set<String> anag = new HashSet<String>();
            if (str == null)     //Handling error scenarios
            {
                return null;
             }
             else if (str.length() == 0)
             {
                anag.add("");
                return anag;
            }
            char initial = str.charAt(0);    // first character
            String rem = str.substring(1);    // Full string without first character
            Set<String> words = anagramsFinder(rem);
            for (String strNew : words)
            {
                for (int i = 0;i<=strNew.length();i++)
                {
                    anag.add(charInsert(strNew, initial, i));
                }
            }
            return anag;
        }
     
        public static String charInsert(String s, char a, int i)
       {
            String begin = s.substring(0, i);
            String end = s.substring(i);
            return begin + a + end;
        }
     
        public static void main(String[] args)
        {
            String s = "XYZ";
            String s1 = "MOM";
            System.out.println("nPermutations for " + s + " are: n" + permutationFinder(s));
            System.out.println("nPermutations for " + s1 + " are: n" + permutationFinder(s1));
        }
    }

    Output:

    Permutations for XYZ are:
    YXZ, YZX, XYZ, XZY, ZXY, ZYX

    Permutations for MOM are:
    OMM, MOM, MMO

  • import java.util.*;
    class AnagramsWord 
    {
        int k = 0;
         
        void input()throws Exception
        {
            Scanner sy = new Scanner(System.in);
            System.out.print("Enter a word : ");   \input the word to find anagrams
            String ana = sy.next();
            System.out.println("The Anagrams are : ");
            display("",ana);
            System.out.println("Total Number of Anagrams = "+k);
        }
       
        void display(String m1, String m2)
        {
            if(m2.length()<=1)
            {
                k++;
                System.out.println(m1+m2);
            }
            else
            {
                for(int i=0; i<m2.length(); i++)       \list the Anagrams word
                {
                    String a = m2.substring(i, i+1);
                    String b = m2.substring(0, i);
                    String c = m2.substring(i+1);
                    display(m1+a, b+c);
                }
            }
        }
         
        public static void main(String args[])throws Exception
        {
            AnagramsWord ob=new AnagramsWord();
            ob.input();
        }
    }

    Output:

    Enter a word : ABCD

    The Anagrams are :
    ABCD
    CADB
    BCAD
    BACD
    DABC
    ABDC
    DCBA
    ADBC
    ADCB
    DBAC
    CBDA
    CBAD
    DACB
    ACBD
    CDBA
    CDAB
    DCAB
    ACDB
    DBCA
    BDAC
    CABD
    BADC
    BCDA
    BDCA
    Total Number of Anagrams = 24

Also See:   library classes and library functions in Java
  • Write a Program in Java to check for anagrams
  • Anagrams Generator in Java

Check Also

Java program to implement polymorphism

Java program to implement polymorphism: Polymorphism is a technique in java with which you can ...

Leave a Reply

Your email address will not be published. Required fields are marked *