Why my Java version exceed the time limit?


  • -1
    J

    I have seen many versions that uses HashMap and I wonder if it works if I don't use HashMap.But my version got Time exceed Limit.

     if(s==null||t==null) {
                return s==t;
            } 
            if("".equals(s)||"".equals(t)){
                return s.equals(t);
            }
            char[] charS=s.toCharArray();
            char[] charT=t.toCharArray();
            boolean flag=true;
            for(int i=0;i<charS.length&&flag;i++) {
                char a=charS[i];
                char b=charT[i];
                for(int j=i+1;j<charS.length;j++) {
                    if(charT[j]==b){
                        if(charS[j]!=a){
                        	flag=false;
                        	break;
                        }
                    }
                }
            }
           return flag;

  • 0
    C

    Since your code is taking O(n^2) time complexity, OJ is throwing time exceeded error. The logic looks ok but they are accepting something in O(n) time.


Log in to reply
 

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