首页 > 其他 > 详细

LeetCode – Refresh – One Edit Distance

时间:2015-03-21 16:54:15      阅读:364      评论:0      收藏:0      [点我收藏+]

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

 

LeetCode – Refresh – One Edit Distance

原文:http://www.cnblogs.com/shuashuashua/p/4355656.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!