Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- bool Check(const string& slong, const string& sshort) {
- int n = sshort.size();
- int idx = 0;
- while (idx < n && sshort[idx] == slong[idx]) {
- idx++;
- }
- while (idx < n && sshort[idx] == slong[idx + 1]) {
- idx++;
- }
- return idx == n;
- }
- bool OneEditApart(const string& s1, const string& s2) {
- if (s1.size() == s2.size()) {
- int left_difference_idx = 0;
- while (left_difference_idx < s1.size() && s1[left_difference_idx] == s2[left_difference_idx]) {
- left_difference_idx++;
- }
- int right_difference_idx = s1.size() - 1;
- while (right_difference_idx >= 0 && s1[right_difference_idx] == s2[right_difference_idx]) {
- right_difference_idx--;
- }
- if (left_difference_idx == s1.size()) {
- return false;
- }
- return left_difference_idx == right_difference_idx;
- } else if (s1.size() == s2.size() + 1){
- return Check(s1, s2);
- } else if (s2.size() == s1.size() + 1){
- return Check(s2, s1);
- }
- return false;
- }
- int main() {
- cout << OneEditApart("cat", "cat") << endl;
- cout << OneEditApart("#", "") << endl;
- cout << OneEditApart("", "#") << endl;
- cout << OneEditApart("123", "1234") << endl;
- cout << OneEditApart("cat", "at") << endl;
- cout << OneEditApart("cat", "cut") << endl;
- cout << OneEditApart("12errjfasjlf", "cdfdfdut") << endl;
- cout << OneEditApart("", "cdfdfdut") << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement