/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package misspellings; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** * * @author Michael */ public class Misspellings { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(new File("misspelling.in")); PrintWriter out = new PrintWriter(new File("misspelling.out")); while(in.hasNext()){ int dictSize = in.nextInt(); in.nextLine(); String[] dict = new String[dictSize]; for (int i = 0; i < dictSize; i++){ dict[i] = in.nextLine(); } int numWords = in.nextInt(); in.nextLine(); for (int l = 0; l < numWords; l++){ String check = in.nextLine(); //System.out.println("Checking " + check); boolean found = false; for (int i = 0; i < dict.length; i++){ //correct if(check.length() == dict[i].length()){ // System.out.println("entered correct"); // System.out.println("Check = " + check); //System.out.println("dict[i] =" + dict[i] ); if (check.equals(dict[i])){ out.println(check + " is correct"); found =true; } // System.out.println("Misses: " + misses); } //done for correct } //TOP OF DICT[i] LOOP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! for (int i = 0; i < dict.length; i++){ //Check for swaps if (check.length() == dict[i].length() && sameLetters(dict[i], check) && !found){ //System.out.println("Checking " +check+ " vs " + dict[i]); int[] nonMatch = {-1, -1}; boolean overflow = false; for (int j = 0; j < check.length(); j++){ if(check.charAt(j) != dict[i].charAt(j)){ if(nonMatch[0] == -1){ nonMatch[0] = j; }else if(nonMatch[1] == -1){ nonMatch[1] = j; }else{ overflow = true; } } } if(nonMatch[1] != -1 && !overflow){ if(Math.abs( nonMatch[0] - nonMatch[1]) == 1){ out.println(check + " is a misspelling of " + dict[i]); found = true; } } } //done checking for swaps //check for 1 letter wrong if(check.length() == dict[i].length() && !found){ // System.out.println("entered misses"); int misses = 0; for (int j = 0; j < check.length(); j++){ if(check.charAt(j) != dict[i].charAt(j)){ misses ++; } } if (misses == 1){ out.println(check + " is a misspelling of " + dict[i]); found = true; break; } // System.out.println("Misses: " + misses); } //done for 1 letter wrong or correct //off by 1 if(Math.abs(check.length() - dict[i].length()) == 1 && !found){ boolean answer = false; if(check.length() > dict[i].length()){ answer = oneLess(check, dict[i]); }else{ answer = oneLess(dict[i], check); } if(answer){ out.println(check + " is a misspelling of " + dict[i]); found = true; } } } if (!found){ out.println(check + " is unknown"); } } } out.close(); in.close(); } public static boolean oneLess(String longer, String shorter){ int i = 0; int j = 0; int sim = 0; while (j < shorter.length() && i < longer.length()){ if(shorter.charAt(j) == longer.charAt(i)){ sim++; i++; j++; }else{ i++; } } if (sim == longer.length() -1){ return true; }else{ return false; } } public static boolean sameLetters(String a, String b){ boolean out = true; String[] array = a.split(""); for (int i = 0; i < b.length(); i++){ boolean toggle = false; for (int j = 0; j < array.length; j++){ String c = "" + b.charAt(i); if (c.equals(array[j])){ toggle = true; } } if (toggle == false){ out = false; } } return out; } }