easily understand code, you see and know


  • 1
    N
    public boolean isOneEditDistance(String s, String t) {
            if(Math.abs(s.length()-t.length())>1){
                return false;
            }
            int edit = 0;
            int len1 = s.length();
            int len2 = t.length();
            
            int i = 0;
            int j = 0;
            while(i<len1 && j<len2){
                if(s.charAt(i)!=t.charAt(j)){
                    edit++;
                    if(len1 != len2){
                        if(len1>len2){
                            i++;
                        }else{
                            j++;
                        }
                    }else{
                        i++;
                        j++;
                    }
                }else{
                   i++;
                   j++; 
                }
                if(edit>1){
                    return false;
                }
                
            }
            if(edit == 0 && len1!=len2){
                return true;
            }
            return edit ==1;
            
        }
    

Log in to reply
 

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