title: Given two strings, find if they are one edit away from each other
body: <p>I came across this question recently:</p>
  
  <pre><code>Given two strings, return true if they are one edit away from each other,else return false.
  An edit is insert/replace/delete a character. 
  Ex. {"abc","ab"}-&gt;true, {"abc","adc"}-&gt;true, {"abc","cab"}-&gt;false
  </code></pre>
  
  <p>One way to solve this problem would be to find the edit distance between the two strings using dynamic programming, and check if it is 1. That would take O(N2) time. Is there a way to do this in linear time, as we've to only check if they are 1 edit away?</p>
  
  <p>The code I wrote below works for most cases, but fails for cases like {"m",""}</p>
  
  <pre><code>public boolean isOneEditDistance(String s1,String s2){
      if(s1==null || s2==null)
          return true;
      if(s1.equals(s2))
          return false;
      if (Math.abs(s1.length() - s2.length()) &gt; 1)
          return false;
      int i = -1;
      int j = -1;
      while (true)
      {
          i++;
          j++;
          if (i == s1.length())
              return true;
          if (s1.charAt(i) == s2.charAt(j))
              continue;
          if (i != j)
              return false;
          if (s1.length() &lt; s2.length())
              i--;
          else
              j--;
      }
  }
  </code></pre>