Advertisement
goshansmails

Untitled

Mar 6th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool Check(const string& slong, const string& sshort) {
  5.     int n = sshort.size();
  6.     int idx = 0;
  7.     while (idx < n && sshort[idx] == slong[idx]) {
  8.         idx++;
  9.     }
  10.     while (idx < n && sshort[idx] == slong[idx + 1]) {
  11.         idx++;
  12.     }
  13.     return idx == n;
  14. }
  15.  
  16. bool OneEditApart(const string& s1, const string& s2) {
  17.     if (s1.size() == s2.size()) {
  18.         int left_difference_idx = 0;
  19.         while (left_difference_idx < s1.size() && s1[left_difference_idx] == s2[left_difference_idx]) {
  20.             left_difference_idx++;
  21.         }
  22.  
  23.         int right_difference_idx = s1.size() - 1;
  24.         while (right_difference_idx >= 0 && s1[right_difference_idx] == s2[right_difference_idx]) {
  25.             right_difference_idx--;
  26.         }
  27.  
  28.         if (left_difference_idx == s1.size()) {
  29.             return false;
  30.         }
  31.  
  32.         return left_difference_idx == right_difference_idx;
  33.     } else if (s1.size() == s2.size() + 1){
  34.         return Check(s1, s2);
  35.     } else if (s2.size() == s1.size() + 1){
  36.         return Check(s2, s1);
  37.     }
  38.  
  39.     return false;
  40. }
  41.  
  42. int main() {
  43.     cout << OneEditApart("cat", "cat") << endl;
  44.     cout << OneEditApart("#", "") << endl;
  45.     cout << OneEditApart("", "#") << endl;
  46.     cout << OneEditApart("123", "1234") << endl;
  47.     cout << OneEditApart("cat", "at") << endl;
  48.     cout << OneEditApart("cat", "cut") << endl;
  49.     cout << OneEditApart("12errjfasjlf", "cdfdfdut") << endl;
  50.     cout << OneEditApart("", "cdfdfdut") << endl;
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement