开发者社区> jianchao_li> 正文

[LeetCode] One Edit Distance

简介: Problem Description: Given two strings S and T, determine if they are both one edit distance apart.
+关注继续查看

Problem Description:

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

To solve this problem, you first need to know what is edit distance. You may refer to this wikipedia article for more information.

For this problem, it implicitly assumes to use the classic Levenshtein distance, which involvesinsertion, deletion and substitution operations and all of them are of the same cost. Thus, if Sis one edit distance apart from TT is automatically one edit distance apart from S.

Now let's think about the possible cases for two strings to be one edit distance apart. Well, that means, we can transform S to T by using exactly one edit operation. There are three possible cases:

  1. We insert a character into S to get T.
  2. We delete a character from S to get T.
  3. We substitute a character of S to get T.

For cases 1 and 2, S and T will be one apart in their lengths. For cases 3, they are of the same length.

It is relatively easy to handle case 3. We simply traverse both of them and compare the characters at the corresponding position. If we find exactly one mismatch during the traverse, they are one edit distance apart.

Now let's move on to cases 1 and 2. In fact, they can be merged into one case, that is, to delete a character from the long string to get the short one, or equivalently, to insert a character into the short string to get the long one.

We will handle cases 1 and 2 using the short string as the reference. We traverse the two strings, once we find a mismatch. We know this position is where the deletion in the long string happens. For example, suppose S = "kitten" and T = "kiten", we meet the first mismatch in the 4-th position, which corresponds to the deleted character below, shown in between *. We then continue to compare the remaining string of T (en) with the remaining string of S (en) and find them to be the same. So they are one edit distance apart.

S: k i t t e n

T: k i t *t* e n

In fact, cases 1, 2 and 3 can be further handled using the same piece of code. For strings of the same length, once we find a mismatch, we just substitute one to be another and check whether they are now the same. For strings of one apart in lengths, we insert the deleted character of the long string into the short one and compare whether they are the same.

The code is as follows. If you find the first half of the return statement (!mismatch && (n - m == 1)) hard to understand, run the code on cases that the mismatch only occurs at the last character of the long string, like S = "ab" and T = "abc".

 1 class Solution {
 2 public:
 3     bool isOneEditDistance(string s, string t) {
 4         int m = s.length(), n = t.length();
 5         if (m > n) return isOneEditDistance(t, s);
 6         if (n - m > 1) return false;
 7         bool mismatch = false;
 8         for (int i = 0, j = 0; i < m; i++, j++) {
 9             if (s[i] != t[j]) {
10                 if (m == n) s[i] = t[j];
11                 else s.insert(i, 1, t[j]);
12                 mismatch = true;
13                 break;
14             }
15         }
16         return (!mismatch && (n - m == 1)) || (mismatch && s == t);
17     }
18 };

 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
LeetCode 72. Edit Distance
给定两个单词word1和word2,找到将word1转换为word2所需的最小操作数。 您对单词允许以下3个操作: 插入一个字符 删除一个字符 替换一个字符
14 0
LeetCode之Hamming Distance
LeetCode之Hamming Distance
58 0
[LeetCode] Shortest Word Distance
Problem Description: Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
799 0
[LeetCode] Edit Distance
This is a classic problem of Dynamic Programming. We define the state dp[i][j] to be the minimum number of operations to convert word1[0.
703 0
+关注
jianchao_li
热爱人工智能、机器学习和算法;截至2018年1月2日,LeetCode用户Most Reputation排名第2(https://discuss.leetcode.com/users?section=sort-reputation)
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载