Anagrams in Java

Published: March 20, 2019

This program will grab all of the words from a dictionary at the path specified location.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Anagram {
    private static String[] dictonary;
    private static ArrayList<String> anagrams = new ArrayList<String>();

    public static void main(String[] args) {
        dictonary = getWordsFromDictionary();
        sortWithCanolicalForm(dictonary);

        System.out.println("Enter a word: ");
        Scanner console = new Scanner(System.in);
        String word = console.next();
        console.close();

        // search up and down
        int index = indexOfWordInDictionary(word);

        searchUpForAnagrams(index);
        searchDownForAnagrams(index);

        System.out.println("Anagrams : " + anagrams);
    }

    private static int indexOfWordInDictionary(String word) {
        for (int i = 0; i < dictonary.length; i++) {
            if (dictonary[i].compareToIgnoreCase(word) == 0) {
                return i;
            }
        }
        return -1;
    }

    private static void searchUpForAnagrams(int start) {
        if (start + 1 > dictonary.length)
            return;

        char[] startChars = dictonary[start].toCharArray();
        char[] endChars = dictonary[start + 1].toCharArray();

        Arrays.sort(startChars);
        Arrays.sort(endChars);

        String endWord = "", startWord = "";
        for (Character ch : endChars) {
            endWord += ch;
        }
        for (Character ch : startChars) {
            startWord += ch;
        }
        if (endWord.compareToIgnoreCase(startWord) == 0) {
            anagrams.add(dictonary[start + 1]);
            searchUpForAnagrams(start + 1);
        }
    }

    private static void searchDownForAnagrams(int start) {
        if (start - 1 < 0)
            return;

        char[] startChars = dictonary[start].toCharArray();
        char[] endChars = dictonary[start - 1].toCharArray();

        Arrays.sort(startChars);
        Arrays.sort(endChars);

        String endWord = "", startWord = "";
        for (Character ch : endChars) {
            endWord += ch;
        }
        for (Character ch : startChars) {
            startWord += ch;
        }
        if (endWord.compareToIgnoreCase(startWord) == 0) {
            anagrams.add(dictonary[start - 1]);
            searchDownForAnagrams(start - 1);
        }
    }

    // gets all the words from the dictionary .txt file
    private static String[] getWordsFromDictionary() {
        List<String> words = new ArrayList<String>();
        try {
            Scanner dictionary = new Scanner(new File("Dictionary.txt"));
            while (dictionary.hasNext()) {
                words.add(dictionary.next());
            }
            dictionary.close();
        } catch (FileNotFoundException r) {
            r.printStackTrace();
        }
        String[] arry = new String[words.size()];
        arry = words.toArray(arry);

        return arry;
    }

    private static void sortWithCanolicalForm(String[] dict) {

        Arrays.sort(dict, new Comparator<String>() {
            public int compare(String o1, String o2) {
                char[] ch1 = o1.toCharArray();
                char[] ch2 = o2.toCharArray();

                Arrays.sort(ch1);
                Arrays.sort(ch2);

                o1 = new String(ch1);
                o2 = new String(ch2);

                return o1.compareToIgnoreCase(o2);
            }
        });
    }


Share this article