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

 

posted on 2015-03-21 15:17  keepshuatishuati  阅读(126)  评论(0编辑  收藏  举报