Super concise 9-line C++ code


  • 0
    R
    class Solution {
    public:
        bool isOneEditDistance(string s, string t) {
            int slen = s.length(), tlen = t.length(), i = 0, j = 0;
            if (slen < tlen) return isOneEditDistance(t, s);
            if (slen - tlen > 1) return false;
            int diffCount = slen==tlen ? 0 : 1;
            while (j < tlen)
                if (s[i++] != t[j++]) {
                    if (slen == tlen || i != j) diffCount++;
                    else j--;
                }
            return diffCount==1;
        }
    };
    

Log in to reply
 

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