Simple neat and easy to understand code


  • 0
    S
    public class Solution {
       public boolean isOneEditDistance(String s, String t) {
            if (Math.abs(s.length() - t.length())> 1) return false;
            int i = 0;
            for (; i < s.length() && i < t.length(); i++) {
                if (s.charAt(i) != t.charAt(i)) break;
            }
            if (i == Math.min(s.length(),t.length()))
                return Math.abs(s.length() - t.length()) == 1;
            boolean case1 = s.substring(i+1).equals(t.substring(i+1));
            boolean case2 = s.substring(i+1).equals(t.substring(i));
            boolean case3 = s.substring(i).equals(t.substring(i+1));
            return case1||case2||case3;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.