My easy to understood Java solution.


  • 0
    S
    public class Solution {
        public boolean isOneEditDistance(String s, String t) {
            if (s == null || t == null) {
                return false;
            }
            if (Math.abs(s.length() - t.length()) > 1) {
                return false;
            }
            int i  = 0;
            int j = 0;
            while (i < s.length() && j < t.length() && s.charAt(i) == t.charAt(j)) {
                i++;
                j++;
            }
            if (i == s.length() && j == t.length()) {
                return false;
            } else if (i == s.length() || j == t.length()){
                    return true;
            }
            return s.substring(i + 1).equals(t.substring(j + 1)) || s.substring(i).equals(t.substring(j + 1)) || s.substring(i + 1).equals(t.substring(j));
        }
    }
    

Log in to reply
 

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