O(n) Accepted C# Easy understand


  • 0
    S
    public class Solution {
    public bool IsOneEditDistance(string s, string t) {
        int lens=s.Length;
        int lent=t.Length;
        if(Math.Abs(lens-lent)>1)return false;
        int i=0;int j=0;int count=0;
        while(i<lens&&j<lent){
            if(s[i]!=t[j]){
                if(++count>1)return false;
                if(lens>lent){
                    i++;continue;
                }else if(lent>lens){
                    j++;continue;
                }
            }
            i++;j++;
        }
        if(count==0&&(i<lens||j<lent))return true;
        return count==1;
    }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.