Simple Java solution


  • 7
    Y
    public class Solution {
    public boolean isOneEditDistance(String s, String t) {
        if(Math.abs(s.length() - t.length()) > 1)  return false;
        int i = 0, j = 0,err = 0;
        while(i<s.length() && j<t.length())
        {
            if(s.charAt(i) != t.charAt(j))
            {
                err++;
                if(err > 1)
                    return false;
                if(s.length() > t.length())
                    j--;
                else if(s.length() < t.length())
                    i--;
            }
            i++;
            j++;
        }
        return (err == 1 || (err == 0 && t.length() != s.length()))? true: false;
    }
    

    }


  • 0
    C

    awesome solution.


  • 0
    Y

    Thank you. :)


Log in to reply
 

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