Leetcode Longest Substring Without Repeating Characters Time Limit Exceeded


  • 0
    S
    enter code herepublic class Solution {
    public int lengthOfLongestSubstring(String s) {
        String str2 = ""; 
    	List<String> list = new ArrayList<String>();
    	for (int i = 0; i < s.length()-1; i++) {
    		str2 = s.charAt(i)+"";
    		inner:for (int j = i+1; j < s.length(); j++) {
    			if (str2.indexOf(s.charAt(j))==-1) {
    				str2+=s.charAt(j);
    			}else{
    				break inner;
    			}
    		}
    		list.add(str2);
    	}
    	int num = 0;
    	if (list.size()>0) {
    		num = list.get(0).length();
    	}
    	for (String string : list) {
    		if (string.length() > num) {
    			num = string.length();
    		}
    	}
    	return num;
    }
    

    }


Log in to reply
 

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