C++ easy understand


  • 0
    class Solution {
    public:
        bool isOneEditDistance(string s, string t) {
            if(s==t) return false;
        	int sizeDiff= s.size()-t.size();
        	if(sizeDiff<0) return isOneEditDistance(t, s); 
        	if(sizeDiff>1) return false;
        	int safeZone=1;
        	if(sizeDiff==0)
        	{
        		for(int i=0; i<t.size(); i++)
        		{
        			if(s[i]!=t[i])
        			{
        				if(safeZone) safeZone--;
        				else return false;
        			}
        		}
        	}
        	else //==1
        	{
        		for(int i=0; i<t.size(); i++)
        		{
        			if(s[i+1-safeZone]!=t[i])
        			{
        				if(safeZone && s[i+1]==t[i]) safeZone--;
        				else return false;
        			}
        		}
        	}
        	return true;
        }
    };

Log in to reply
 

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