The Java code always get TLE, please check OJ.


  • 5
    A

    I think my java code is O(n). But I always get TLE on the longest test cases.
    I also tried to all the accepted java solution from Discuss to OJ, but all of them got TLE.
    I guess that leetcode OJ may has some problem on java. Please check it.

    public class Solution {
        public boolean isOneEditDistance(String s, String t) {
            int m=s.length();
            int n=t.length();
            if(m>n){
                return isOneEditDistance(t,s);
            }
            if(m==n){
                int count=0;
                for(int i=0;i<n;i++){
                    if(s.charAt(i)!=t.charAt(i)){
                        count++;
                    }
                }
                return count==1; 
            }else if(m==n-1){
                int count=0;
                int i=0;
                while(i<m){
                    if(s.charAt(i)!=t.charAt(i)){
                        return s.substring(i, m).equals(t.substring(i+1, n));
                    }
                    i++;
                }
                return true;
            }
            return false;
        }
    }

  • 0
    J

    Same here.
    This is my java code, also got TLE.

    public class Solution {
        public boolean isOneEditDistance(String s, String t) {
            int m = s.length();
            int n = t.length();
            if(m > n){
                return isOneEditDistance(t, s);
            }
            if(n - m > 1){
                return false;
            }
            int distance = n - m;
            int i = 0;
            while(i < m && s.charAt(i) == t.charAt(i)){
                i++;
            }
            if(i == m){
                return distance > 0;
            }
            if(distance == 0){
                i++;
            }
            while(i < m && s.charAt(i) == t.charAt(i+distance)){
                i++;
            }
            return i==m;
        }
    }

  • 0
    F

    Got AC after submitting a couple of times.. sometimes when you are lucky it can be accpeted...


  • 0

    Thanks for your feedback. I've just fixed this issue by increasing the time limit.


Log in to reply
 

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