// All the words are in the file Dictionary.txt
import java.io.*;
import java.util.*;
public class Anag
{
static String[] words;
public static void main(String args[]) throws IOException
{
Hashtable<String, List> ht;
try {
FileInputStream fstream = new FileInputStream("Dictionary.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
List<String> lines = new ArrayList<String>();
String ele;
while ((ele = br.readLine()) != null)
lines.add(ele);
words = lines.toArray(new String[lines.size()]);
in.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
ht = getAnagrams();
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter key number:");
String wd = br1.readLine();
char[] chars = wd.toCharArray();
Arrays.sort(chars);
String key = String.valueOf(chars);
System.out.println(ht.get(key));
}
public static Hashtable<String, List> getAnagrams() {
Hashtable<String, List> ht = new Hashtable<String, List>();
Set<String> hasAnagram = new HashSet<String>();
for (String wd : words) {
if (wd.length() <= 1)
continue;
// sort the characters in this word
char[] chars = wd.toCharArray();
Arrays.sort(chars);
String anaKey = String.valueOf(chars);
// update the list of anagrams
List<String> anagramList = null;
if (ht.containsKey(anaKey)) {
anagramList = ht.get(anaKey);
hasAnagram.add(anaKey);
}
if (anagramList == null) {
anagramList = new ArrayList<String>();
anagramList.add(wd);
ht.put(anaKey, anagramList);
}
if (!anagramList.contains(wd)) {
anagramList.add(wd);
}
}
return ht;
}
}
import java.io.*;
import java.util.*;
public class Anag
{
static String[] words;
public static void main(String args[]) throws IOException
{
Hashtable<String, List> ht;
try {
FileInputStream fstream = new FileInputStream("Dictionary.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
List<String> lines = new ArrayList<String>();
String ele;
while ((ele = br.readLine()) != null)
lines.add(ele);
words = lines.toArray(new String[lines.size()]);
in.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
ht = getAnagrams();
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter key number:");
String wd = br1.readLine();
char[] chars = wd.toCharArray();
Arrays.sort(chars);
String key = String.valueOf(chars);
System.out.println(ht.get(key));
}
public static Hashtable<String, List> getAnagrams() {
Hashtable<String, List> ht = new Hashtable<String, List>();
Set<String> hasAnagram = new HashSet<String>();
for (String wd : words) {
if (wd.length() <= 1)
continue;
// sort the characters in this word
char[] chars = wd.toCharArray();
Arrays.sort(chars);
String anaKey = String.valueOf(chars);
// update the list of anagrams
List<String> anagramList = null;
if (ht.containsKey(anaKey)) {
anagramList = ht.get(anaKey);
hasAnagram.add(anaKey);
}
if (anagramList == null) {
anagramList = new ArrayList<String>();
anagramList.add(wd);
ht.put(anaKey, anagramList);
}
if (!anagramList.contains(wd)) {
anagramList.add(wd);
}
}
return ht;
}
}
Comments
Post a Comment