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(string s, string t) {
 4         if (s.size() > t.size()) {
 5             return isOneEditDistance(t, s);
 6         }
 7
 8         if (t.size() - s.size() > 1) return false;
 9         bool found = false;
10         for (int i = 0, j = 0; j < t.size(); i++, j++) {
11             if (s[i] != t[j]) {
12                 if (found) return false;
13                 found = true;
14                 if (t.size() > s.size()) {
15                     i--;
16                 }
17             }
18         }
19         return (found || t.size() > s.size());
20     }
21 };
时间: 2025-01-06 11:23:47

LeetCode – Refresh – One Edit Distance的相关文章

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 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

(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[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(

【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

problem: 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 character b) Delete a character

[Leetcode 72]编辑距离 Edit Distance

[题目] 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: Input: word1

[LeetCode] One Edit Distance 一个编辑距离

Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance的拓展,然而这道题并没有那道题难,这道题只让我们判断两个字符串的编辑距离是否为1,那么我们只需分下列三种情况来考虑就行了: 1. 两个字符串的长度之差大于1,那么直接返回False 2. 两个字符串的长度之差等于1,那么长的那个字符串去掉一个字符,剩下的应该和短的字符串相同 3. 两个字符串的长度之