package com.tacoscott.chapter01;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by scott.powers on 3/21/17.
 */
public class q5 {
  //Start @ 5:43pm
  //My re-wording of question:
  //  Given two strings, check to see if the second is one minor edit away from the first.
  //  Minor edits are considered a single insert/delete/replace of a char
  //Questions I would ask:
  //  None that I'm away
  //Initial Thoughts:
  //If the string lengths vary by greater than one, return false


  private static boolean firstPass(String input1, String input2) {
    if (Math.abs(input1.length() - input2.length()) > 1) { return false; }
    boolean foundEdit = false;
    for(int i=0;i<input1.length();i++) {
      if (input2.length() < i) {
        char a = input1.charAt(i);
        char b = input2.charAt(i);
        if (a != b) {
          if (foundEdit) {
            return false;
          } else {
            foundEdit = true;
          }
        }
      }
    }
    return foundEdit;
  }


  private static void passThru(String input1, String input2) {
    System.out.println("Minor Edit Detector Detector: '" + input1 + "' '"+input2+"' -> " + firstPass(input1,input2));
  }

  public static void main(String[] args) {

    passThru("tacobell","tacbell"); //delete
    passThru("tacobell","tacobbell"); //insert
    passThru("tacobell","tacogell"); //replace
  }
}

@@ -1001,16 +1001,1761 @@
 t;%0A  %7D%0A%0A
+  //Restarted at 10:23am the next day%0A  //Dropping firstPass and starting over fresh.%0A%0A  private static boolean secondPass(String input1, String input2) %7B%0A    boolean possibleAdd = input1.length() - input2.length() == - 1;%0A    boolean possibleDelete = input1.length() - input2.length() == 1;%0A    boolean possibleReplace = input1.length() == input2.length();%0A%0A    if (!(possibleAdd %7C%7C possibleDelete %7C%7C possibleReplace)) %7B%0A      return false;%0A    %7D%0A%0A    int i1 = 0;%0A    int i2 = 0;%0A%0A    boolean foundSingleChange = false;%0A    while (i1 %3C input1.length() && i2 %3C input2.length()) %7B%0A%0A      char a = input1.charAt(i1);%0A      char b = input2.charAt(i2);%0A      if (a != b) %7B%0A        if (foundSingleChange) %7B return false; %7D%0A        foundSingleChange = true;%0A%0A        if (possibleAdd) %7B%0A          i2++;%0A        %7D else if (possibleDelete) %7B%0A          i2--;%0A        %7D%0A      %7D%0A      i1++;%0A      i2++;%0A    %7D%0A    return foundSingleChange;%0A  %7D%0A  //End @ 10:35am%0A  //Hints used: none%0A  //Thoughts before looking at solution in book:%0A  //  Generally pretty happy this secondPass. It short circuits out if it's an obvious major edit, and can identify the%0A  //  type of edit it's likely to see/allow.%0A  //  The incrementing read indexes seem reasonable, and the while loop protect array out of bounds.%0A  //Thoughts after looking at solution(s) in book:%0A  //  My solution is very simialr to their refinement of their first solution. Using read advancing read indexes.%0A  //  I pre-identify the type of minor edit that would be allowed based on the string length diff, and use that to%0A  //  return early if it meets no criteria. I then use that pre-identified type of edit to advance the indexes rather%0A  //  than do the calulate the string length diff every loop.%0A
 %0A  priva
@@ -2849,17 +2849,8 @@
 dit 
-Detector 
 Dete
@@ -2892,21 +2892,22 @@
  -%3E %22 + 
-first
+second
 Pass(inp
@@ -3107,15 +3107,174 @@
 /replace
+%0A%0A    passThru(%22tacobell%22,%22tacobell%22); //no change%0A    passThru(%22tacobell%22,%22tacobellll%22); //too many changes%0A    passThru(%22tacobell%22,%22tac%22); //too many changes
 %0A  %7D%0A%7D%0A

solution 1.5