nikunjsoni

161

Jun 16th, 2021
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     bool isOneEditDistance(string s, string t) {
  4.         int m = s.size(), n = t.size();
  5.         for (int i = 0; i < min(m, n); i++) {
  6.             if(s[i] != t[i]){
  7.                 if(m == n) // Replace char.
  8.                     return s.substr(i + 1) == t.substr(i + 1);
  9.                 else if(m > n) // Delete char.
  10.                     return s.substr(i+1) == t.substr(i);
  11.                 else // Insert char.
  12.                     return s.substr(i) == t.substr(i+1);
  13.             }
  14.         }
  15.         return abs(m-n) == 1;
  16.     }
  17. };
Advertisement
Add Comment
Please, Sign In to add comment