码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode: One Edit Distance

时间:2015-01-24 06:44:38      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

Given two strings S and T, determine if they are both one edit distance apart.

用i, j 两个指针,从左边扫到右边,两个一起走。

s.charAt(i) != t.charAt(j)

distance++

要么i跳一格,要么j跳一格,要么i,j一起跳。 分别对应着:insert, delete, replace.

然后如果distance >1, return false;

base 情况有三: 

1. i、j都走到各自string.length(), 由于之前只要dist>1就会return false, 所以现在肯定没有dist>1, 所以直接return true

2. i走到s的最右边,j还没有,所以当前dist要加上t.length()-j这一段。

3. j走到t的最右边,i还没有,所以当前dist要加上s.length()-i这一段。

没做oj不知道是否会过,O(N)扫描一次,不算recursion stack空间复杂度O(1)

 1 public class Solution {  
 2    public boolean isOneEditDistance(String s, String t) {
 3        if (s==null || t==null) return false;
 4        return check(s, t, 0, 0, 0);
 5    }
 6    
 7    public boolean check(String s, String t, int i, int j, int dist) {
 8        if (i == s.length() && j == t.length()) return true;
 9        else if (i == s.length()) {
10            dist += t.length() - j;
11            return dist>1? false : true;
12        }
13        else if (j == t.length()) {
14            dist += s.length() - i;
15            return dist>1? false : true;
16        }
17        if (s.charAt(i) != t.charAt(j)) {
18            dist++;
19            if (dist > 1) return false;
20            return check(s, t, i+1, j, dist) || check(s, t, i, j+1, dist) || check(s, t, i+1, j+1, dist);
21        }
22        else return check(s, t, i+1, j+1, dist);
23    }
24 }

别人的做法:

 1 public class Solution {
 2    public boolean isOneEditDistance(String s, String t) {
 3        return check(s,t,0,0,0);
 4   }
 5   
 6   public boolean check(String s, String t, int i, int j, int distance){
 7       while(0<=i && i<=s.length()-1 && 0<=j && j<=t.length()-1){
 8           if(s.charAt(i) != t.charAt(j)){
 9               distance++;
10               if(distance>1) return false;
11               return check(s,t,i+1,j,distance) || check(s,t,i,j+1,distance) || check(s,t,i+1,j+1,distance);
12           }else{
13               return check(s,t,i+1,j+1,distance);
14           }
15       }
16       
17       if(distance ==1){
18           return i==s.length() && j==t.length();
19       }else { //(distance ==0)
20           return Math.abs(s.length()-t.length())==1;
21       }
22   }
23 }

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

 1 public class Solution {
 2    public boolean isOneEditDistance(String s, String t) {
 3        if (s==null || t==null) return false;
 4        int[][] res = new int[s.length()+1][t.length()+1];
 5        res[0][0] = 0;
 6        for (int i=1; i<=s.length(); i++) {
 7            res[i][0] = i;
 8        }
 9        for (int j=1; j<=t.length(); j++) {
10            res[0][j] = j;
11        }
12        for (int i=1; i<=s.length(); i++) {
13            for (int j=1; j<=t.length(); j++) {
14                int minOftwo = Math.min(res[i-1][j], res[i][j-1]) + 1;
15                int replace = s.charAt(i-1) == t.charAt(j-1)? res[i-1][j-1] : res[i-1][j-1]+1;
16                res[i][j] = Math.min(minOftwo, replace);
17                if (res[i][j] >= 2) return false;
18            }
19        }
20        return true;
21    }
22 }

 

Leetcode: One Edit Distance

标签:

原文地址:http://www.cnblogs.com/EdwardLiu/p/4245378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!