Simple JS solution


  • 0
    V
    var isOneEditDistance = function(s, t) {
        
        if (t.length > s.length) {
            let temp = t;
            t = s;
            s = temp;
        }
        
        let start =0;
        let end =0;
        let count =0;
        let maxDist = 0;
        
        while (end < s.length) {
            
            if(s[end+start] !== t[end]) {
                count++;
            }
            while (count>0) {
                if (maxDist > 1 ) return false;
    
                if(s.length > t.length && s[end+1+start] == t[end]) {
                    start++;
                }
                
                maxDist++;
                count--;
            }
            
            end++;
        }
        
        return maxDist ===1? true: false;
    };
    

Log in to reply
 

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