[Leetcode] One edit Distance an editing distance

Source: Internet
Author: User

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

This problem is the extension of the previous edit distance, however, this problem is not the problem, this problem only let us determine whether the editing distance of two strings is 1, then we only need to divide the following three cases to consider the line:

1. Two strings with a difference of length greater than 1, then return false directly

2. The difference in length of two strings is equal to 1, then the long string is removed by one character, and the remainder should be the same as the short string

3. The difference in length of two strings is equal to 0, so there can be only one difference in the character of the two string corresponding to the position.

After analyzing all the cases, the code is very well written, see below:

Solution One:

classSolution { Public:    BOOLIsoneeditdistance (stringSstringt) {if(S.size () <t.size ()) swap (S, t); intm = S.size (), n = t.size (), diff = m-N; if(diff >=2)return false; Else if(diff = =1) {             for(inti =0; I < n; ++i) {if(S[i]! =T[i]) {                    returnS.SUBSTR (i +1) ==t.substr (i); }            }            return true; } Else {            intCNT =0;  for(inti =0; I < m; ++i) {if(S[i]! = T[i]) + +CNT; }            returnCNT = =1; }    }};

We can actually make the code more concise, only need to compare two strings corresponding to the characters in the position, if we encounter different time, then we look at the length of the two string relationship, if equal, then we compare the current position after the string is the same, if the length of S is large, So we compare the substring at the beginning of the next position of S, and the substring at the beginning of T's current position is the same, and if the length of T is large, then we compare the substring starting at the next position of T, and the substring starting at the current position of S is the same. If the loop ends and no different characters are found, then let's see if the length of the two string is 1, see the code below:

Solution Two:

classSolution { Public:    BOOLIsoneeditdistance (stringSstringt) { for(inti =0; I < min (S.size (), t.size ()); ++i) {if(S[i]! =T[i]) {                if(s.size () = = T.size ())returnS.SUBSTR (i +1) = = T.substr (i +1); Else if(S.size () < T.size ())returnS.SUBSTR (i) = = T.substr (i +1); Else returnS.SUBSTR (i +1) ==t.substr (i); }        }        returnABS (S.size ()-t.size ()) = =1; }};

Similar topics:

Edit Distance

Resources:

Https://leetcode.com/discuss/71071/my-clear-java-solution-with-explanation

Leetcode all in one topic summary (continuous update ...)

[Leetcode] One edit Distance an editing 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.