Leetcode One Edit Distance

Source: Internet
Author: User

The original title link is here: https://leetcode.com/problems/one-edit-distance/

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

Similar to edit distance.

If the length difference is greater than 1, return false. If the length difference equals 1, when you encounter different char, the long one is moved backwards. If the lengths are equal, move backwards while encountering different char.

The loop has not returned, that is, so far the same, then see if the length difference is equal to 1. This equals 0, which means it's exactly the same or not.

Time Complexity:o (Math.min (Len1, Len2)). Space:o (1).

AC Java:

1  Public classSolution {2      Public Booleanisoneeditdistance (string s, String t) {3         if(s = =NULL|| t = =NULL){4             return false;5         }6         intLen1 =s.length ();7         intLen2 =t.length ();8          for(inti = 0; I < Math.min (len1, len2); i++){9             if(S.charat (i)! =T.charat (i)) {Ten                 if(Len1 = =len2) { One                     returnS.substring (i+1). Equals (T.substring (i+1)); A}Else if(Len1 >len2) { -                     returnS.substring (i+1). Equals (T.substring (i)); -}Else{ the                     returnS.substring (i). Equals (T.substring (i+1)); -                 } -             } -         } +         returnMath.Abs (len1-len2) = = 1; -     } +}

Leetcode One Edit Distance

Related Article

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.