In a spell checker it is useful to check whether the given w
In a spell checker, it is useful to check whether the given word is one symbol away from a word in the dictionary. For a language L, define L\' to the set of all strings obtainable by altering at most one symbol in a string of L. For example, if L is {CAT, DOG}, then L\' is {AAT, BAT, CAT, . . . , ZAT, . . . , AOG, . . . , DOZ}. Show how to convert an FA for L into one for L\'.
Thanks in advance.
Solution
AlterOneLetter.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
public class AlterOneLetter {
public static void main(String args[]) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
HashSet<String> originalList = new HashSet<String>();
System.out.println(\"++++++++++++++++++ Input some Strings (Press control + Z to finish) : +++++++++++++++++++\");
String word = reader.readLine();
while (word != null) {
if(!word.isEmpty()) {
originalList.add(word.toUpperCase());
}
word = reader.readLine();
}
LinkedHashSet<String> resultantList = new LinkedHashSet<String>();
Iterator<String> originalListIterator = originalList.iterator();
System.out.println(\"\ ++++++++++++++++++ Printing the Input list (L): +++++++++++++++++++\");
while(originalListIterator.hasNext()) {
System.out.println(originalListIterator.next());
}
// reinitialize original list iterator
originalListIterator = originalList.iterator();
while(originalListIterator.hasNext()) {
// get the current input string
String str = originalListIterator.next();
// one by one replace all the characters of original String
// i is the index of current character of string, to which we
// want to replace
for(int i=0; i<str.length(); i++) {
// Change current character from A-Z one by one and
// put in resultant list
for(char c=\'A\'; c<=\'Z\'; c++) {
resultantList.add(returnAfterReplacement(str, i, c));
}
}
}
System.out.println(\"\ ++++++++++++++++++ Printing the resultant list (L`): +++++++++++++++++++\");
Iterator<String> iterator = resultantList.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
public static String returnAfterReplacement(String str, int index, char c) {
StringBuilder stringBuilder = new StringBuilder(str);
stringBuilder.setCharAt(index, c);
return stringBuilder.toString();
}
}

