Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.


    public int lengthOfLongestSubstring(String s) {
        Set <Character> set = new HashSet();
        char [] sl = s.toCharArray();
        int b = 0, e = 0, max = 0; 
        while(e < sl.length){
            if(set.add(sl[e])){
                max = Math.max(e - b + 1, max);
            }else{
                while(sl[b] != sl[e]){
                    // remove b before b increase 1
                    set.remove(sl[b ++]);
                }
                b ++;
            }
            e ++;
        }
        return max;
    }

results matching ""

    No results matching ""