4-Lines Super Concise Soluion


  • 2
    O
    public boolean isOneEditDistance(String s, String t) {
        int lens = s.length(), lent = t.length(), i = 0, j = 0;
        for (; i < lens && i < lent && s.charAt(i) == t.charAt(i); i++);
        for (; j < lens && j < lent && s.charAt(lens - 1 - j) == t.charAt(lent - 1 - j); j++);
        return (lens == lent && i + j + 1 == lens) || (Math.abs(lens - lent) == 1 && i + j + 1 == Math.max(lens, lent));
    }

Log in to reply
 

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