Solution 1

Java
// // Best Solution
public int lengthOfLongestSubstring(String s) {
    Set<Character> set = new HashSet<>();
    int left = 0, right = 0, max = 0;
    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;
}
Leet Code/java.java · L429–445

Solution 2

Java
// // Easy to understand
public int lengthOfLongestSubstring2(String s) {
    Set<Character> set = new HashSet<>();
    int left = 0, right = 0, max = 0;
    while ( right < s.length() ) {
        while (set.contains(s.charAt(right))) {
            set.remove(s.charAt(left));
            left++;
        }
        set.add(s.charAt(right));
        max = Math.max(max, set.size());
        right++;
    }
    return max;
}
Leet Code/java.java · L446–461

Solution 3

Java
// // Easy to understand - Set is optimized
public int lengthOfLongestSubstring3(String s) {
    boolean[] visited = new boolean[256];
    int left = 0, max = 0;
    for (int right = 0; right < s.length(); right++) {
        while (visited[s.charAt(right)]) {
            visited[s.charAt(left)] = false;
            left++;
        }
        visited[s.charAt(right)] = true;
        max = Math.max(max, right-left+1);
    }
    return max;
}
Leet Code/java.java · L462–476