Java Anagram Solutions

I can decide how to create line anagrams, but I don’t know how I can compare them with 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?

+4
source share
6 answers

No, but you can get a list of words from various places . From there you can read the word list file in the 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.

+5
source

One method for determining whether a character set is an anagram of a word involves primes. Assign a prime number to each letter, for example a = 2, b = 3, c = 5, d = 7. Now precommute the product of primes for each word in the dictionary. For example, add = 2 * 7 * 7 = 98 or bad = 3 * 2 * 7 = 42.

Now, determining whether a set of letters is an anagram of any word in the dictionary can be done by calculating the value of the set of letters. For example, the letters "abd" = 2 * 3 * 7 = 42 = 'bad'. Just check if the computed value for the letters exists in your precomputed dictionary. For any anagram, you only need to do this calculation once against trying to generate all possible anagrams. Please note, however, this method will only work for relatively small words, otherwise you will run into overflow problems and will have to use BigInteger.

+2
source

No, you need to use an external library such as JWNL , which is a wrapper for WordNet - a machine-readable lexical database organized by values ​​that contains almost every English word.

+1
source

Perhaps the English jazzy dictionary might help you.

+1
source

There is no such specialized class in the standard Java library, but you can use any implementation that you like in the Set interface and initialize it by loading it with words of your choice, selected from any of the countless word lists , you can find in many places (just check carefully that the license for the word list you have chosen is compatible with your intended application, for example, does it allow commercial use, closed source applications, if required, and etc.).

+1
source
  import java.util.Scanner; public class AnagramsString { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter First a String: "); String str1 = in.next(); System.out.println("Enter Second String :"); String str2 = in.next(); char ch1[] = str1.toCharArray(); int lenstr1 = ch1.length; char ch2[] = str2.toCharArray(); int lenstr2 = ch2.length; int count = 0; if(lenstr1==lenstr2) { for(int i=0;i<lenstr1;i++) { for(int j=0;j<lenstr2;j++) { if(ch1[i]==ch2[j]) { count++; ch2[j]='0'; }//if }//inner for }//outer for if(count==lenstr1) { System.out.println("Both Strings are Anagrams"); }//if else { System.out.println("Sorry! Both Strings are Not Anagrams"); } }//if length else { System.out.println("Inputed Strings are not Anagrams!"); }//else }//main }//class enter code here 
-3
source

Source: https://habr.com/ru/post/1307881/


All Articles