In: Computer Science
here i have a dictionary with the words and i have to find the words with the largest size, (i mean word count for eg abdominohysterectomy) which have anagrams , like for example ,loop -> pool. Also , i have to sort them in alphabetical way so what could be the algorithm for that
public class Anagrams {
private static final int MAX_WORD_LENGTH = 25;
private static IArray<IVector<String>>
theDictionaries =
new
Array<>(MAX_WORD_LENGTH);
public static void main(String... args) {
initTheDictionaies();
process();
}
private static void process() {
//start here
if (isAnagram("pool", "loop"))
{
// REMOVE
System.out.println("ANAGRAM!");
//
REMOVE
//above this
}
private static boolean isAnagram(String word1, String
word2) {
boolean b = false;
if
(getWordSorted(word1).equals(getWordSorted(word2))) {
b = true;
}
return b;
}
private static String getWordSorted(String word)
{
ISortedList<Character>
sortedLetters = new SortedList<>();
StringBuilder sb = new
StringBuilder();
for (int i = 0; i <
word.length(); i++) {
sortedLetters.add(word.charAt(i));
}
for (Character c : sortedLetters)
{
sb.append(c);
}
String sortedWord =
sb.toString();
return sortedWord;
}
private static void initTheDictionaies() {
String s;
for (int i = 0; i <
theDictionaries.getSize(); i++) {
theDictionaries.set(i, new Vector<String>());
}
try (
BufferedReader
br = new BufferedReader(
new FileReader("data/pocket.dic")
)
) {
while ((s =
br.readLine()) != null) {
theDictionaries.get(s.length()).pushBack(s);
}
} catch (Exception ex) {
ex.printStackTrace();
System.exit(-1);
}
for (int i = 0; i <
theDictionaries.getSize(); i++) {
theDictionaries.get(i).shrinkToFit();
}
}
}
/*************************process()************************/
private static void process() {
//start here
//set the word size to min
int wordSize =
Integer.MIN_VALUE;
//To store the largest word
String largetWord = "";
//get the all sorted word in
string
String wordsString =
getWordSorted(word);
//create and fill the array by
words
String[] word = wordsString.split("
");
for (int i = 0;
i < word.length; i++) {
//compare word size
if(wordSize<word[i].length()) {
wordSize =
word[i].length();
//save the largest word
largetWord =
largetWord+word[i];
}
}
System.out.println("The largest word is "+largetWord+" And having
size "+wordSize);
//above this
}
Thanks a lot, Please let me know if you have any problem....