1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| class Solution { public: bool isDelete(string a, string b){ for(int i = 0; i < a.size(); i++){ string str = a; str.erase(i, 1); if(str == b) return true; } return false; }
bool oneEditAway(string first, string second) { int firstLen = first.size(); int secondLen = second.size(); if(first == second) return true; else if(abs(firstLen - secondLen) > 1) return false; else if(abs(firstLen - secondLen) == 1) { if(firstLen > secondLen) return isDelete(first, second); else return isDelete(second, first); } else { int num = 0; for(int i = 0; i < first.size(); i++){ if(first[i] != second[i]){ num ++; } } return num == 1 || num == 0 ? true : false; } } };
|