Simple and Easy to understand solution [WITHOUT USING EXTRA SPACE]


  • 0
    B
    public int lengthOfLongestSubstring(String s) {
    		int max = 0;
    		if (s == null || s.length() == 0)
    			return max;
    		max = 1;
    		int start = 0;
    		int currMax = 1;
    		for (int i = 1; i < s.length(); i++) {
    			char ch = s.charAt(i);
    			String subStr = s.substring(start, i);
    
    			if (subStr.indexOf(ch) == -1) {
    				currMax++;
    				max = Math.max(max, currMax);
    				continue;
    			}
    			start = s.lastIndexOf(ch, i - 1) + 1;
    			currMax = i - start + 1;
    			max = Math.max(currMax, max);
    		}
    
    		return max;
    	}
    

Log in to reply
 

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