您的位置:首页 > 其它

LeetCode – Refresh – One Edit Distance

2015-03-21 15:17 417 查看
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.

class Solution {
public:
bool isOneEditDistance(string s, string t) {
if (s.size() > t.size()) {
return isOneEditDistance(t, s);
}

if (t.size() - s.size() > 1) return false;
bool found = false;
for (int i = 0, j = 0; j < t.size(); i++, j++) {
if (s[i] != t[j]) {
if (found) return false;
found = true;
if (t.size() > s.size()) {
i--;
}
}
}
return (found || t.size() > s.size());
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: