> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/01-24-2022-3.md).

# 01/24/2022 3

Sliding windows. using two pointers left and right:  pointing the first char of string. move right pointer and if the char is not duplicate, add it into a set, computer the max value, increment the right pointer. . If it is duplicate, then remove the left char and increment the left pointer.&#x20;

Time O(N)

Space O(N)

class Solution {&#x20;

public int lengthOfLongestSubstring(String s) {&#x20;

if(s == null || s.length() == 0) return 0;

```
    int left = 0, right = 0, max = 0;
    Set<Character> set = new HashSet<>();
    
    while(right < s.length()){
        if(!set.contains(s.charAt(right))){
            set.add(s.charAt(right));
            max = Math.max(max, set.size());
            right++;
        }else{
            set.remove(s.charAt(left));
            left++;
        }
        
    }
    return max;
    
}
```

}
