您的位置:首页 > 其它

Leetcode: One Edit Distance

2015-01-24 05:46 399 查看
Given two strings S and T, determine if they are both one edit distance apart.


注意这道题:Must be exactly one distance apart. Not the same.

推荐方法:

public class Solution {
public boolean isOneEditDistance(String s, String t) {
if (s.length()==0 && t.length()==0) return false;
if (s.length()+t.length()==1) return true;

if (Math.abs(s.length()-t.length())>1) return false;

if (s.length()==t.length()) return isOneReplace(s,t);

if (s.length()>t.length()) return isOneInsert(s,t);
else return isOneInsert(t,s);

}

public boolean isOneInsert(String a, String b){
//a is 1 char longer than b, we determine whether a and b is one insertion distance.

boolean modified = false;

int index1 = 0;
int index2 = 0;

while (index2<b.length()){
if (a.charAt(index1)==b.charAt(index2)){
index1++;
index2++;
} else {
if (modified) return false;
else {
index1++;
modified = true;
}
}
}
return true;
}

public boolean isOneReplace(String a, String b){
//a and b have the same length, we determine whether they are one replace aparted.
boolean modified = false;
int index1=0;
while (index1<a.length())
if (a.charAt(index1)==b.charAt(index1))
index1++;
else if (modified) return false;
else {
index1++;
modified = true;
}
if (modified) return true;
else return false;
}

}


别人用Recursion做的这道题,感觉第7行那个while 应该改成if,仅供参考

public class Solution {
public boolean isOneEditDistance(String s, String t) {
return check(s,t,0,0,0);
}

public boolean check(String s, String t, int i, int j, int distance){
while(0<=i && i<=s.length()-1 && 0<=j && j<=t.length()-1){
if(s.charAt(i) != t.charAt(j)){
distance++;
if(distance>1) return false;
return check(s,t,i+1,j,distance) || check(s,t,i,j+1,distance) || check(s,t,i+1,j+1,distance);
}else{
return check(s,t,i+1,j+1,distance);
}
}

if(distance ==1){
return i==s.length() && j==t.length();
}else { //(distance ==0)
return Math.abs(s.length()-t.length())==1;
}
}
}


另外这道题类似edit distance,可以用那个方法做,但是会TLE

public class Solution {
public boolean isOneEditDistance(String s, String t) {
if (s==null || t==null) return false;
int[][] res = new int[s.length()+1][t.length()+1];
res[0][0] = 0;
for (int i=1; i<=s.length(); i++) {
res[i][0] = i;
}
for (int j=1; j<=t.length(); j++) {
res[0][j] = j;
}
for (int i=1; i<=s.length(); i++) {
for (int j=1; j<=t.length(); j++) {
int minOftwo = Math.min(res[i-1][j], res[i][j-1]) + 1;
int replace = s.charAt(i-1) == t.charAt(j-1)? res[i-1][j-1] : res[i-1][j-1]+1;
res[i][j] = Math.min(minOftwo, replace);
if (res[i][j] >= 2) return false;
}
}
return res[s.length][t.length]==1? true : false;    }
}


另外FB面经有一道比较狠的这个题的变形:

class IntFileIterator {
boolean hasNext();
int next();
}

class{
public boolean isDistanceZeroOrOne(IntFileIterator a, IntFileIterator b);

}
// return if the distance between a and b is at most 1..
// Distance: minimum number of modifications to make a=b
// Modification:
//   1. change an int in a
//   2. insert an int to a
//   3. remove an int from a


这题就是one edit distance的变形题,难点在于给的Iterator,事先不知道两个file
的长度,也不允许用extra space(所以不能转成两个string再比),那么一个个往前
跑的时候就得三种情况都考虑。。。。

我的做法:

public class Solution {
class IntFileIterator {
boolean hasNext();
int next();
}

public boolean isDistanceZeroOrOne(IntFileIterator a, IntFileIterator b) {
return check(a, b, 0);
}

public boolean check(IntFileIterator a, IntFileIterator b, int distance){
IntFileIterator aa = new InFileIterator(a); // copy of iterator a before next() function
IntFileIterator bb = new InFileIterator(b);
while (a.hasNext() && b.hasNext()) {
int s = a.next();
int t = b.next();
if(s != t){
IntFileIterator aaa = new InFileIterator(a); //copy of iterator a after next() function
IntFileIterator bbb = new InFileIterator(b);
distance++;
if(distance>1) return false;
return check(aa, b, distance) || check(a, bb, distance) || check(aaa, bbb, distance);
}
else{
return check(a, b, distance);
}
}

if(distance == 1){
return !a.hasNext() && !b.hasNext();
}else { //(distance ==0)
IntFileIterator k = a.hasNext()? a : b;
int count = 0;
while (k.hasNext()) {
k.next();
count++;
}
return k<=1;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: