Skip to content

Instantly share code, notes, and snippets.

@jaydonnell
Created August 14, 2009 17:47
Show Gist options
  • Save jaydonnell/167984 to your computer and use it in GitHub Desktop.
Save jaydonnell/167984 to your computer and use it in GitHub Desktop.
public class WordDistance {
public static ArrayList<String> getPossibleEdits(String word) {
// returns set of all candidate words that are one edit away from entered word
ArrayList<String> possibleEdits = new ArrayList<String>();
for(int i=0; i < word.length(); ++i) possibleEdits.add(word.substring(0, i) + word.substring(i+1));
for(int i=0; i < word.length()-1; ++i){
String tmp = word.substring(0, i) + word.substring(i+1, i+2) + word.substring(i, i+1) + word.substring(i+2);
if(!tmp.equals(word)) possibleEdits.add(tmp);
}
for(int i=0; i < word.length(); ++i) {
for(char c='a'; c <= 'z'; ++c) {
String tmp = word.substring(0, i) + String.valueOf(c) + word.substring(i+1);
if(!tmp.equals(word)) possibleEdits.add(tmp);
}
}
for(int i=0; i <= word.length(); ++i) {
for(char c='a'; c <= 'z'; ++c) {
String tmp = word.substring(0, i) + String.valueOf(c) + word.substring(i);
if(!tmp.equals(word)) possibleEdits.add(tmp);
}
}
return possibleEdits;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment