Wednesday, January 21, 2015

Day 93, ##, One Edit Distance

One Edit Distance

Given two strings S and T, determine if they are both one edit distance apart.
-------------------------------------------------------
edit之后可以之后对剩下的string取等式
class Solution {
public:
    bool isOneEditDistance(string s, string t) {
        int d = s.size() - t.size();
        if (abs(d) > 1 || s == t) return false; 
        
        for (int i = 0; i < s.length(); i++) {
            if (i < t.length() && s[i] != t[i]) {
                return s.substr(i + 1) == t.substr(i + 1) 
                        || s.substr(i + 1) == t.substr(i)
                        || s.substr(i) == t.substr(i + 1);
            }
        }
        
        return true;
    }
};

No comments:

Post a Comment