RainX_69

ONE EDIT DISTANCE | TRICKY | OA PROBLEM | MUST DO

Feb 26th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | Source Code | 0 0
  1. https://www.lintcode.com/problem/640/description
  2.  
  3. Given two strings S and T, determine if they are both one edit distance apart.
  4. One ediit distance means doing one of these operation:
  5.  
  6. insert one character in any position of S
  7. delete one character in S
  8. change any one character in S to other character
  9. Wechat reply 【Two Sigma】 get the latest requent Interview questions. (wechat id : jiuzhang1104)
  10.  
  11.  
  12. Example
  13.  
  14. Example 1:
  15. Input: s = "aDb", t = "adb"
  16. Output: true
  17. Example 2:
  18.  
  19. Input: s = "ab", t = "ab"
  20. Output: false
  21. Explanation:
  22. s=t ,so they aren't one edit distance apart
  23.  
  24. ---------------------------------------------------------------------------------------------------------------------------------------
  25.  
  26. class Solution {
  27. public:
  28.    bool isOneEditDistance(string s, string t) {
  29.        int m=s.size();
  30.        int n=t.size();
  31.        if(abs(m-n)>1){
  32.            return false;
  33.        }
  34.        for(int i=0;i<min(m,n);i++){
  35.            if(s[i]==t[i]){
  36.                continue;
  37.            }
  38.            bool REPLACE=s.substr(i+1) == t.substr(i+1);
  39.            bool DELETE=s.substr(i+1) == t.substr(i);  
  40.            bool INSERT=s.substr(i) == t.substr(i+1); // eg s=bcde , t=abcde
  41.            return REPLACE || DELETE | INSERT;
  42.        }
  43.        return abs(n-m)==1;  // example s=a , t=ab
  44.    }
  45. };
Advertisement
Add Comment
Please, Sign In to add comment