C++ easy solution


  • 0
        bool isOneEditDistance(string s, string t) {
            int m = s.size();
            int n = t.size();
            if(m - n > 1) return false;
            if(n > m) return isOneEditDistance(t, s);
            int i_s = 0;
            int i_t = 0;
            int count = 0;
            while(i_s < m){
                if(s[i_s] != t[i_t]){ 
                    count++;
                    if(m > n) i_t--;
                }
                i_s++;
                i_t++;
            }
            return count == 1 && (i_s == n + 1 || i_s == n);
        }
    

Log in to reply
 

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