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();
}
时间: 2024-12-16 03:20:57

leetcode[161] One Edit Distance的相关文章

leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java

Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到的. 在这里需要注意几点: 1.不等于1的变换都要返回false(包括变换次数等于0). 2.还有很多细节需要注意. 方法如下: 1.直接判断:1)如果差值大于1,直接返回false.  2)如果长度相同,那么依次判断,是否只有一个字母不一样.  3)如果不一样,那么看是否是只是多出了一个字母. p

72. Edit Distance &amp;&amp; 161. One Edit Distance

72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a characterb) Delete a c

【LeetCode】161. One Edit Distance

Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if they are both one edit distance apart. Intuition 同时遍历比较S和T的字符,直至遇到不同的字符:如果S和T的字符个数相同时,跳过不同的那个字符,继续遍历:如果S和T的字符个数相差为1时,跳过较长的字符串的当天字符,继续遍历.如果剩下的字符都相等,那么返回tr

161. One Edit Distance

题目: Given two strings S and T, determine if they are both one edit distance apart. 链接: http://leetcode.com/problems/one-edit-distance/ 6/14/2017 2ms, 58% 提交好几次才通过,所以代码像打补丁一样.注意的test case: 2个空string -- false 相同的string -- false 当检查出来不一致时,根据长度来比较之后的subs

Baozi Leetcode solution 72. Edit Distance

Problem Statement Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1:

(LeetCode 72)Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a characterb) Delete a characterc) Replace

【一天一道LeetCode】#72. Edit Distance

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given two words word1 and word2, find the minimum number of steps ?>required to convert word1 to word2. (each operation is counted as 1 step.) You have the f

【LeetCode 72】Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a characterb) Delete a characterc) Replace

LeetCode – Refresh – One Edit Distance

Scanning from start to end. If find a mismatch and one is larger size, keep search from the previous char of shorter one. Finally check whether found a mismatch OR still have a larger size string. 1 class Solution { 2 public: 3 bool isOneEditDistance