Inspired by the "Regular Expression Matching", 3ms


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

Log in to reply
 

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