码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 161: One Edit Distance

时间:2017-12-18 12:05:28      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:false   return   i++   log   distance   ring   amp   class   while   

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

标签:false   return   i++   log   distance   ring   amp   class   while   

原文地址:http://www.cnblogs.com/liangmou/p/8056181.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!