Simple C++ one loop solution


  • 0
    X
        bool isOneEditDistance(string s, string t) {
            if (s.size() > t.size()) return isOneEditDistance(t, s);
            if (t.size() - s.size() > 1 || s == t) return false;
            for (int i = 0; i < s.size(); ++i) {
                if (s[i] != t[i]) {
                    if (t.size() == s.size()) {
                        return s.substr(i+1) == t.substr(i+1);
                    } else {
                        return s.substr(i) == t.substr(i+1);
                    }
                }
            }
            return true;
        }
    

Log in to reply
 

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