Simple Java solution, 2ms


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

Log in to reply
 

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