Leetcode:one Edit Distance

Source: Internet
Author: User

if They is both one edit distance apart.

With I, j two pointers, sweep from left to right, two go together.

S.charat (i)! = T.charat (j)

distance++

Either I jump one cell, J jumps a grid, or i,j jumps together. Corresponding to: INSERT, delete, replace.

Then if distance >1, return false;

The base case has three:

1. I, J all go to their respective string.length (), because as long as the dist>1 will return false, so there is certainly no dist>1, so the direct return to True

2. I go to the far right of S, J has not, so the current dist to add t.length ()-j this paragraph.

3. J go to the far right of T, I have not, so the current dist to add s.length ()-I this paragraph.

Did not do OJ do not know whether will be over, O (N) scan once, not calculate recursion stack space complexity O (1)

1  Public classSolution {2     Public Booleanisoneeditdistance (string s, String t) {3        if(s==NULL|| t==NULL)return false;4        returnCheck (s, t, 0, 0, 0);5    }6    7     Public BooleanCheck (string s, String T,intIintJintDist) {8        if(i = = S.length () && j = t.length ())return true;9        Else if(i = =s.length ()) {TenDist + = T.length ()-J; One            returnDist>1?false:true; A        } -        Else if(J = =t.length ()) { -Dist + = s.length ()-i; the            returnDist>1?false:true; -        } -        if(S.charat (i)! =T.charat (j)) { -dist++; +            if(Dist > 1)return false; -            returnCheck (s, T, I+1, J, dist) | | Check (s, T, I, j+1, dist) | | Check (s, T, I+1, j+1, Dist); +        } A        Else returnCheck (s, T, I+1, j+1, Dist); at    } -}

Other people's approach:

1  Public classSolution {2     Public Booleanisoneeditdistance (string s, String t) {3        returnCheck (s,t,0,0,0);4   }5   6    Public BooleanCheck (string s, String T,intIintJintdistance) {7        while(0<=i && i<=s.length ()-1 && 0<=j && j<=t.length ()-1){8           if(S.charat (i)! =T.charat (j)) {9distance++;Ten               if(distance>1)return false; One               returnCheck (s,t,i+1,j,distance) | | Check (s,t,i,j+1,distance) | | Check (s,t,i+1,j+1, distance); A}Else{ -               returnCheck (s,t,i+1,j+1, distance); -           } the       } -        -       if(Distance ==1){ -           returnI==s.length () && j==t.length (); +}Else{//(distance ==0) -           returnMath.Abs (S.length ()-t.length ()) ==1; +       } A   } at}

In addition, this problem is similar to edit distance, can be done with that method, but will tle

1  Public classSolution {2     Public Booleanisoneeditdistance (string s, String t) {3        if(s==NULL|| t==NULL)return false;4        int[] res =New int[S.length () +1] [T.length () +1];5Res[0][0] = 0;6         for(intI=1; I<=s.length (); i++) {7Res[i][0] =i;8        }9         for(intJ=1; J<=t.length (); J + +) {TenRES[0][J] =J; One        } A         for(intI=1; I<=s.length (); i++) { -             for(intJ=1; J<=t.length (); J + +) { -                intMinoftwo = Math.min (Res[i-1][j], res[i][j-1]) + 1; the                intReplace = 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 true; +    } A}

Leetcode:one Edit Distance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.