Search code examples
javastringdictionarysolveranagram

Java Anagram Solver


I can work out how to create anagrams of a string but I don't know how I can compare them to a dictionary of real words to check if the anagram is a real word. Is there a class in the Java API that contains the entire English dictionary?


Solution

  • No, but you can get a wordlist from various places. From there, you could read the wordlist file into a list:

    List<String> lines = new ArrayList<String>();
    BufferedReader in = new BufferedReader(new FileReader("wordlist.txt"));
    String line = null;
    while (null!=(line=in.readLine()))
    {
       lines.add(line);
    }
    in.close();
    

    And finally binary search use lines.contains() for your candidate word.