6 Lines Solution, shortest version!


  • 0
    X
    bool isOneEditDistance(string s, string t) 
        {
            if(s.size()<t.size())
                swap(s,t);
            for(int i=0;i<t.size();i++)
                if(s[i]!=t[i])
                    return s.size()==t.size()?s.substr(i+1)==t.substr(i+1):s.substr(i+1)==t.substr(i);
            return s.size()-t.size()==1;
        }
    

Log in to reply
 

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