1. 程式人生 > >Leetcode 161: One Edit Distance

Leetcode 161: One Edit Distance

nbsp one determine spa ++ solution math ive ngs

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