Simpla Java 2ms Solution


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

Log in to reply
 

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