Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Code to solve multi word anagrams
Java
branch: master
Failed to load latest commit information.
src update javadoc comments
wordlist first commit
README.md update README.md with LICENSE
pom.xml first commit

README.md

Anagram Solver

A multi word anagram solver

Build

git clone https://github.com/parekhparth/AnagramSolver.git

cd AnagramSolver

mvn clean install

(above command runs the test and builds the 1.0-SNAPSHOT jar)

Execute

after you build the jar, you can run the anagram solver using following:

Usage:

java -cp AnagramSolver.jar com.parthparekh.algorithms.AnagramSolver <absolute_path_to_wordlist_file> <min_word_length> <words_for_anagram_search>

for e.g. if you want to find all the anagrams for "eleven plus two" with minimum 3 letter words, and wordlist file under /tmp/wordlist.txt, you need to run:

java -cp AnagramSolver.jar com.parthparekh.algorithms.AnagramSolver /tmp/wordlist.txt 3 eleven plus two

(you can download the wordlist from here => http://www.sil.org/linguistics/wordlists/english/)

Logic

Word Dictionary

(refer SortedWordDictionary.java for this)

Dictionary is a Map with sorted characters as key and all the corresponding words as value (Set of strings)

Anagram Solver

(refer AnagramSolver.java for this)

Programs first loads only those words in dictionary that are subsets of the words for which all the anagrams are to be found. After that it iterates through each word from dictionary key and does a recursive forward lookup to see if any valid anagram is found for that key. It will only do forward lookup because, anagrams for all the words that have already been traversed, should have already been found. Once you find a set of valid keys that form anagram, merge all the words that correspond to those keys.

(this solver will only list unique anagrams i.e. "eleven plus two" and "two plus eleven" are similar and it will only list one of them)

Stats

Below time stats are taken from my MacBook Air (8G memory, Intel core i7)

Words Total number of unique anagrams Minimum anagram word length Time taken (real) Comments
"twelve plus one" 2886 3 1.355s
"twelve plus one" 20004 2 2.218s
"anagram solver" 8241 3 1.590s
"anagram solver" 68016 2 3.751s
"astronomers" 2848 3 1.143s
"astronomers" 20279 2 1.904s
"a decimal point" 41249 3 3.272s
"a decimal point" 452695 2 18.094s ran this with -Xmx 2048m

LICENSE

This project is under "Do whatever you want" MIT License => http://www.tldrlegal.com/license/mit-license

Something went wrong with that request. Please try again.