I am getting time limit exceeded on submitting solution but input for the above runs fine when i run in custom input


  • 0
    P

    public class Solution {
    public int lengthOfLongestSubstring(String s) {
    CharSequence prev = "";
    StringBuilder maxSeq = new StringBuilder();
    StringBuilder buf = new StringBuilder();

    	for (int i = 0; i < s.length(); i++) {
    		buf.setLength(0);
    		buf.append(s.charAt(i));
    		
    		for (int j = i; j < s.length(); j++) {
    			if (i != j) {
    				if (buf.indexOf(s.charAt(j) + "") == -1) {
    					buf.append(s.charAt(j));
    					prev = buf.subSequence(0, buf.length());
    				}else {
    					if(prev.length()> maxSeq.length()){
    						maxSeq.setLength(0);
    						maxSeq.append(prev);
    						buf.setLength(0);
    						break;
    					}else{
    						
    						break;
    					}
    				}
    				
    			}
    		}
    	}
    	if(maxSeq.length()==0){
    	    return s.length();
    	}
    	return maxSeq.length();
    	}
    }

Log in to reply
 

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