zoukankan      html  css  js  c++  java
  • leetcode

    题目:

    One Edit Distance

    Given two strings S and T, determine if they are both oneedit distance apart.

    Hint:
    1. If | n – m | is greater than 1, we know immediately both are not one-editdistance apart.
    2. It might help if you consider these cases separately, m == n and m ≠ n.
    3. Assume that m is always ≤ n, which greatly simplifies the conditionalstatements. If m > n, we could just simply swap S and T.
    4. If m == n, it becomes finding if there is exactly one modified operation. Ifm ≠ n, you do not have to consider the delete operation. Just consider theinsert operation in T.


    bool OneEditDistance(const string &s1, const string &s2)
    {
    	if (s1.size() > s2.size())
    		return OneEditDistance(s2, s1);
    	if (s2.size() - s1.size()>1)
    		return false;
    	int i1 = 0, i2 = 0;
    	while (i1 < s1.size())
    	{
    		if (s1[i1] == s2[i2])
    		{
    			++i1;
    			++i2;
    		}
    		else
    			break;
    	}
    	if (i1 == s1.size())
    		return true;
    	++i2;
    	while (i1 < s1.size() && i2<s2.size())
    	{
    		if (s1[i1] == s2[i2])
    		{
    			++i1;
    			++i2;
    		}
    		else
    			break;
    	}
    
    	return i1 == s1.size();
    }




  • 相关阅读:
    Alpha 冲刺 (7/10)
    Alpha 冲刺 (6/10)
    同学录
    Alpha 冲刺 (5/10)
    Letcode刷题总结知识点
    python 基础语法
    Python 文件读写与编码解读
    py2exe界面和程序开发打包
    求职者五险一金解读
    互联网企业程序题总结
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5204537.html
Copyright © 2011-2022 走看看