leetcode[161] One Edit Distance

判断两个字符串的编辑距离是不是1.

两个字符串距离是1的可能性:

1)两个字符串长度相等:s="abc",t="aec";

2)两个字符串长度相差1(两种情况):s="abc",t="abce";或s="abc",t="aebc";

bool isOneEditDistance(string s, string t)
{
    if(s.length()>t.length())swap(s,t);
    if(t.length()-s.length()>1)return false;
    bool dif=false;
    for (int i=0,j=0;i<s.length();i++,j++)
    {
        if (s[i]!=t[j])
        {
            if(dif)return false;
            else 
            {
                dif=true;
                if(s.length()<t.length())i--;
            }
        }
    }
    return dif||s.length()<t.length();
}
原文地址:https://www.cnblogs.com/Vae1990Silence/p/4280621.html