[LeetCode] 161. One Edit Distance

Problem

Given two strings s and t, determine if they are both one edit distance apart.

Note:

There are 3 possiblities to satisify one edit distance apart:

  1. Insert a character into s to get t
  2. Delete a character from s to get t
  3. Replace a character of s to get t

Example 1:

1
2
3
Input: s = "ab", t = "acb"
Output: true
Explanation: We can insert 'c' into s to get t.

Example 2:

1
2
3
Input: s = "cab", t = "ad"
Output: false
Explanation: We cannot get t from s by only one step.

Example 3:

1
2
3
Input: s = "1203", t = "1213"
Output: true
Explanation: We can replace '0' with '1' to get t.

Explanation 1

  1. First, we make s to be the longer length string by comparing s.size() and t.size(), if s.size() is smaller, we swap s and t.

  2. We calculate the diff = s.size() - t.size(). If the diff is greater than or equal to 2, then we immediatly return false.

  3. Else if the diff is equal to 1, then we loop i through t from index 0 to t.size() exclusive. If s[i] != t[i], then we want to remove s[i], in other words, we return ` s.substr(i + 1) == t.substr(i)`.

  4. Else the diff == 0, then we loop i through t from index 0 to t.size() exclusive, and count how many characters are different, and return cnt == 1.

Solution 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
    public boolean isOneEditDistance(String s, String t) {
        if (s.length() < t.length()) {
            String temp = s;
            s = t;
            t = temp;
        }
        int sLen = s.length(), tLen = t.length();
        int diff = sLen - tLen;

        if (diff >= 2) {
            return false;

        } else if (diff == 1) {
            for (int i = 0; i < tLen; i++) {
                if (s.charAt(i) != t.charAt(i)) {
                    return s.substring(i+1).equals(t.substring(i));
                }
            }
            return true;

        } else {
            int cnt = 0;
            for (int i = 0; i < tLen; i++) {
                if (s.charAt(i) != t.charAt(i)) {
                    cnt += 1;
                    if (cnt > 1) return false;
                }
            }
            return cnt == 1;
        }
    }
}

Explanation 2

  1. We can simplify the above code. First we loop through i from 0 to min(s.size(), t.size()) exclusive.

  2. If the current index’s character is different, then we check if s has longer length, then we want to remove s[i], so we return s.substr(i + 1) == t.substr(i).

  3. Else if t has longer length, then we want to remove t[i], so we return s.substr(i) == t.substr(i + 1).

  4. Else if they are the same length, then we remove both s[i] and t[i], in other words, return s.substr(i + 1) == t.substr(i + 1).

  5. Outside the for loop, if the first min(s.size(), t.size()) characters are the same, then s.size() and t.size() must difference by 1, in other words, we return abs((int)s.size() - (int)t.size()) == 1.

Solution 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
    public boolean isOneEditDistance(String s, String t) {
        for (int i = 0; i < Math.min(s.length(), t.length()); i++) {
            if (s.charAt(i) != t.charAt(i)) {
                if (s.length() > t.length()) {
                    return s.substring(i+1).equals(t.substring(i));
                } else if (s.length() < t.length()) {
                    return s.substring(i).equals(t.substring(i+1));
                } else {
                    return s.substring(i+1).equals(t.substring(i+1));
                }
            }
        }

        return Math.abs(s.length() - t.length()) == 1;
    }
}