Easy and Short Java solution


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

Log in to reply
 

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