Java Why my code is wrong for "a","", but it works in eclipse??


  • 0
    B

    Why my code is wrong for "a","", but it works in eclipse??

    public boolean isOneEditDistance(String s, String t) {
        int len_s = s.length();
        int len_t = t.length();
        if(Math.abs(len_s - len_t) > 1||s.equals(t)) return false;
        int count = 0; 
        if(len_s == len_t){
            for(int i = 0; i < len_s; i++){
                if(count > 1) return false;
                if(s.charAt(i) != t.charAt(i) )  count++;
            }
        }
        else{
            if(s == "" || t == "") return true;
            int len =  Math.min(len_s,len_t);
            for(int i = 0,j=0; i < len && j < len; i++,j++){
                if(count > 1) return false;
                if(s.charAt(i) != t.charAt(j)){
                    count++;
                    if(len_s < len_t) i--;
                    else j--;
                }
            }
        }
        return count == 1;
    

    }


  • 0
    G

    Because you should cast the s.length() to (int). Because the s.length() would return none negative numbers.


  • 0
    C

    I got the same problem as you, for this test case.

    It is giving the expected answer on my local machine, but not in leetcode OJ.

    I had this same issue for another problem today, correct answer on my local machine, but leetcode says output is something else. (My local machine answer matches expected).


Log in to reply
 

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