Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://www.lintcode.com/problem/640/description
- Given two strings S and T, determine if they are both one edit distance apart.
- One ediit distance means doing one of these operation:
- insert one character in any position of S
- delete one character in S
- change any one character in S to other character
- Wechat reply 【Two Sigma】 get the latest requent Interview questions. (wechat id : jiuzhang1104)
- Example
- Example 1:
- Input: s = "aDb", t = "adb"
- Output: true
- Example 2:
- Input: s = "ab", t = "ab"
- Output: false
- Explanation:
- s=t ,so they aren't one edit distance apart
- ---------------------------------------------------------------------------------------------------------------------------------------
- class Solution {
- public:
- bool isOneEditDistance(string s, string t) {
- int m=s.size();
- int n=t.size();
- if(abs(m-n)>1){
- return false;
- }
- for(int i=0;i<min(m,n);i++){
- if(s[i]==t[i]){
- continue;
- }
- bool REPLACE=s.substr(i+1) == t.substr(i+1);
- bool DELETE=s.substr(i+1) == t.substr(i);
- bool INSERT=s.substr(i) == t.substr(i+1); // eg s=bcde , t=abcde
- return REPLACE || DELETE | INSERT;
- }
- return abs(n-m)==1; // example s=a , t=ab
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment