Home LeetCode. 3. Longest Substring Without Repeating Characters
Post
Cancel

LeetCode. 3. Longest Substring Without Repeating Characters

[Link] https://leetcode.com/problems/longest-substring-without-repeating-characters/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.*;
class Solution {
    public int lengthOfLongestSubstring(String s) {
        int start = 0;
        int max = 0;
        HashSet<Character> set = new HashSet<Character>();
        for(int i = 0; i < s.length(); i++) {
            while(set.contains(s.charAt(i))) {
                set.remove(s.charAt(start++));
            }
            set.add(s.charAt(i));
            max = Math.max(max, set.size());
        }
        return max;
    }
}
This post is licensed under CC BY 4.0 by the author.