package com.fishercoder.solutions;

/**
 * 161. One Edit Distance
 *
 * Given two strings s and t, determine if they are both one edit distance apart.
 *
 * Note:
 *
 * There are 3 possiblities to satisify one edit distance apart:
 *
 *     Insert a character into s to get t
 *     Delete a character from s to get t
 *     Replace a character of s to get t
 *
 * Example 1:
 * Input: s = "ab", t = "acb"
 * Output: true
 * Explanation: We can insert 'c' into s to get t.
 *
 * Example 2:
 * Input: s = "cab", t = "ad"
 * Output: false
 * Explanation: We cannot get t from s by only one step.
 *
 * Example 3:
 * Input: s = "1203", t = "1213"
 * Output: true
 * Explanation: We can replace '0' with '1' to get t.
 */
public class _161 {
  public static class Solution1 {
    public boolean isOneEditDistance(String s, String t) {
      char[] schar = s.toCharArray();
      char[] tchar = t.toCharArray();

      if (Math.abs(s.length() - t.length()) == 1) {
        char[] longer = (s.length() > t.length()) ? schar : tchar;
        char[] shorter = (longer == schar) ? tchar : schar;

        int diffCnt = 0;
        int i = 0;
        int j = 0;
        for (; i < shorter.length && j < longer.length; ) {
          if (longer[j] != shorter[i]) {
            diffCnt++;
            j++;
          } else {
            i++;
            j++;
          }
        }
        return diffCnt == 1
            || diffCnt
            == 0;//it could be the last char of the longer is the different one, in that case, diffCnt remains to be zero
      } else if (s.length() == t.length()) {
        int diffCnt = 0;
        for (int i = 0; i < s.length(); i++) {
          if (schar[i] != tchar[i]) {
            diffCnt++;
          }
          if (diffCnt > 1) {
            return false;
          }
        }
        return diffCnt == 1;
      }
      return false;
    }
  }
}

@@ -156,19 +156,16 @@
 * Note:%0A
- *%0A
  * There
@@ -223,23 +223,16 @@
 part:%0A *
-%0A *    
  Insert 
@@ -261,20 +261,16 @@
 get t%0A *
-    
  Delete 
@@ -299,20 +299,16 @@
 get t%0A *
-    
  Replace
@@ -713,16 +713,18 @@
  _161 %7B%0A
+  
   public
@@ -745,24 +745,28 @@
 Solution1 %7B%0A
+    
     public b
@@ -814,24 +814,30 @@
  t) %7B%0A      
+    
+  
 char%5B%5D schar
@@ -858,24 +858,30 @@
 ay();%0A      
+   
+   
 char%5B%5D tchar
@@ -903,24 +903,30 @@
 y();%0A%0A      
+  
+    
 if (Math.abs
@@ -955,24 +955,32 @@
 ()) == 1) %7B%0A
+        
         char
@@ -1034,32 +1034,40 @@
  tchar;%0A        
+       
+ 
 char%5B%5D shorter =
@@ -1103,16 +1103,24 @@
 schar;%0A%0A
+        
         
@@ -1140,24 +1140,32 @@
  0;%0A        
+  
+      
 int i = 0;%0A 
@@ -1167,24 +1167,32 @@
  0;%0A        
+   
+     
 int j = 0;%0A 
@@ -1186,24 +1186,32 @@
  int j = 0;%0A
+        
         for 
@@ -1264,24 +1264,34 @@
 %7B%0A          
+      
+    
 if (longer%5Bj
@@ -1301,32 +1301,44 @@
 = shorter%5Bi%5D) %7B%0A
+            
             diff
@@ -1348,32 +1348,44 @@
 ++;%0A            
+    
+        
 j++;%0A          %7D
@@ -1379,24 +1379,34 @@
 ;%0A          
+  
+        
 %7D else %7B%0A   
@@ -1418,13 +1418,33 @@
     
+ 
-i++;%0A
+           i++;%0A        
     
@@ -1443,32 +1443,36 @@
                 
+    
 j++;%0A          %7D
@@ -1462,32 +1462,60 @@
  j++;%0A          
+      
+    %7D%0A                
 %7D%0A        %7D%0A    
@@ -1500,34 +1500,32 @@
       %7D%0A        
-%7D%0A
         return d
@@ -1548,16 +1548,28 @@
         
+       
+     
 %7C%7C diffC
@@ -1571,16 +1571,28 @@
 diffCnt%0A
+            
         
@@ -1707,24 +1707,30 @@
  zero%0A      
+    
+  
 %7D else if (s
@@ -1752,24 +1752,32 @@
 length()) %7B%0A
+        
         int 
@@ -1781,32 +1781,40 @@
 nt diffCnt = 0;%0A
+        
         for (int
@@ -1850,24 +1850,34 @@
 %7B%0A          
+  
+        
 if (schar%5Bi%5D
@@ -1896,32 +1896,44 @@
 ) %7B%0A            
+           
+ 
 diffCnt++;%0A     
@@ -1929,32 +1929,42 @@
 nt++;%0A          
+         
+ 
 %7D%0A          if (
@@ -1955,24 +1955,34 @@
 %7D%0A          
+         
+ 
 if (diffCnt 
@@ -1996,24 +1996,36 @@
             
+         
+   
 return false
@@ -2028,32 +2028,60 @@
 alse;%0A          
+      
+    %7D%0A                
 %7D%0A        %7D%0A    
@@ -2073,27 +2073,24 @@
 %0A       
- %7D%0A
         
 return d
@@ -2073,32 +2073,33 @@
 %0A               
+ 
 return diffCnt =
@@ -2109,24 +2109,36 @@
 ;%0A      
-%7D%0A
+      %7D%0A   
       
+   
 return f
@@ -2147,16 +2147,22 @@
 se;%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 161