Find all anagrams of a word in a file. Input - only file name and word. Output - all set of word in file that are anagrams of word. Write production quality code.
2) For each word in the file, find beta string which is its sorted version ex abd is sorted version of bad, adb and dba. Put this word to that list whose key is this beta string. If it does not exist then create a new list with the beta string as key in map.
Scanner s = new Scanner(new File(args[0]));
while (s.hasNext()) {
String word = s.next();
String alpha = sorting(word);
List<String> l = m.get(alpha);
if (l == null)
m.put(alpha, l=new ArrayList<String>());
l.add(word);
}
List<String> l = m.get(sorting(beta));
for sorting, you can do better than klogk, assuming the words are characters (a-z), counting sort can be an option too!
Read full article from Just Programming...: find all anagrams of a word in a file, Java Code : Amazon Interview
2) For each word in the file, find beta string which is its sorted version ex abd is sorted version of bad, adb and dba. Put this word to that list whose key is this beta string. If it does not exist then create a new list with the beta string as key in map.
Scanner s = new Scanner(new File(args[0]));
while (s.hasNext()) {
String word = s.next();
String alpha = sorting(word);
List<String> l = m.get(alpha);
if (l == null)
m.put(alpha, l=new ArrayList<String>());
l.add(word);
}
List<String> l = m.get(sorting(beta));
for sorting, you can do better than klogk, assuming the words are characters (a-z), counting sort can be an option too!
Read full article from Just Programming...: find all anagrams of a word in a file, Java Code : Amazon Interview