Straightforward, simple C++ solution


  • 0
    S
    bool isOneEditDistance(string s, string t) {
        if (s.size() > t.size())
            swap(s,t);
            
        if (s.size() == 0)
            return t.size() == 1;
            
        if (t.size() - s.size() > 1)
            return false;
        
            
        bool usedEdit = false;
        bool isDelete = s.size() != t.size();
        
        int s_pos = 0;
        int t_pos = 0;
        
        while(t_pos < t.size())
        {
            if (s[s_pos] != t[t_pos])
            {
                if (usedEdit)
                    return false;
                usedEdit = true;
                if (!isDelete)
                    s_pos++;
            }
            else
            {
                s_pos++;
            }
            t_pos++;
        }
        
        return usedEdit;
        
        
    }

Log in to reply
 

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