Leetcode 161: One Edit Distance

Posted Keep walking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 161: One Edit Distance相关的知识,希望对你有一定的参考价值。

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

 

 1 public class Solution {
 2     public bool IsOneEditDistance(string s, string t) {
 3         if (s == null || t == null)
 4         {
 5             return false;
 6         }
 7         
 8         if (Math.Abs(s.Length - t.Length) > 1) return false;
 9         
10         int i = 0, j = 0, diff = 0;
11         
12         while (i < s.Length && j < t.Length)
13         {
14             if (s[i] != t[j])
15             {
16                 if (diff++ > 0) return false;
17                 
18                 if (s.Length == t.Length)
19                 {
20                     i++;
21                     j++;
22                 }
23                 else
24                 {
25                     if (s.Length > t.Length)
26                     {
27                         i++;
28                     }
29                     else
30                     {
31                         j++;
32                     }
33                 }
34             }
35             else
36             {
37                 i++;
38                 j++;
39             }
40         }
41         
42         diff += Math.Abs((s.Length - i) - (t.Length - j));
43         
44         return diff == 1;
45     }
46 }

 

以上是关于Leetcode 161: One Edit Distance的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode161. One Edit Distance

161. One Edit Distance

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

161.One Edit Distance

72. Edit Distance && 161. One Edit Distance

161. One Edit Distance