One Edit Distance

Given two strings S and T, determine if they are both one edit distance apart.


    public boolean isOneEditDistance(String s, String t) {
        if(Math.abs(s.length() - t.length()) > 1 || s.equals(t)) return false;

        if(s.length() >= t.length())
            return helper(s, t);
        else return helper(t, s);
    }

    private boolean helper(String s, String t){
        int diff = s.length() - t.length(), si = 0, ti = 0, editTimes = 1;
        while(si < s.length() && ti < t.length()){
            if(s.charAt(si) == t.charAt(ti)){
                si ++;
                ti ++;
            }else {
                if(diff > 0)
                    si ++;
                else {
                    si ++;
                    ti ++;
                }
                if(-- editTimes < 0) return false;
            }
        }
        return true;
    }

results matching ""

    No results matching ""