one edit distance

bool oneDistance(string s1, string s2)
{
    if (s1.length()<s2.length())
    {
        swap(s1, s2);
    }
    if (s1.length() - s2.length() >1)
        return false;
    bool replace = true;
    if (s1.length() != s2.length())
    {
        replace = false;
    }
    int times = 0;
    for (int i = 0, j = 0; j < s1.length();)
    {
        if (i == s2.length() || s1[j] != s2[i])
        {
            times += 1;
            if (times>1)
                return false;
            if (replace)
            {
                continue;
            }
            else
            {
                ++j;
            }
        }
        else
        {
            ++i;
            ++j;
        }
    }
    return true;
}
 
int main()
{
    auto r = oneDistance("atc", "at");
}

 

posted on 2014-11-23 15:43  fatemaster  阅读(256)  评论(0)    收藏  举报

导航