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

LeetCode – Refresh – One Edit Distance

时间:2015-03-21 16:56:01      阅读:142      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!