Ez Java fast solution


  • 0
    M
        public boolean isOneEditDistance(String s, String t) {
            if ( s == null || t == null || s.equals(t)) return false;
            if (t.length() >= s.length()) {
                String temp = t;
                t = s;
                s = temp;
            }
            for (int i = 0; i < t.length(); i++) {
                if (s.charAt(i) != t.charAt(i)) {
                    String tempS = s.substring(0, i) + s.substring(i+1, s.length());
                    String tempT = t.substring(0, i) + t.substring(i+1, t.length());
                    if (tempS.equals(t) || tempT.equals(s) || tempS.equals(tempT)) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
            return s.length() - t.length() == 1;
        }
    

Log in to reply
 

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