Skip to content

Latest commit

 

History

History
21 lines (15 loc) · 452 Bytes

longest-sub-string.md

File metadata and controls

21 lines (15 loc) · 452 Bytes
        public int lengthOfLongestSubstring(String s) {
          Set<Character> seen = new HashSet<>();
          int i = 0, max = 0;

          for (int j = 0; j < s.length(); j++) {
            char c = s.charAt(j);

            while (seen.contains(c)) {
                seen.remove(s.charAt(i++));
            }

            seen.add(c);
            max = Math.max(max, j - i + 1);
          }

          return max;
        
      }