# LeetCode 3. Longest Substring Without Repeating Characters

## Description

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

Given a string, find the length of the longest substring without repeating characters.

Example 1:

Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.


Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.


Example 3:

Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

## Explanation

We use two pointers technique to solve the problem. One slow pointer i, one fast pointer j.

We also add a HashSet to store the characters which have been visited by j pointer to help detect repeating characters.

We keep moving j pointer right further.

• If current s.charAt(j) character is not in the HashSet, we add the character to the HashSet and keep moving j further.
• If current s.charAt(j) character is in the HashSet, we remove the character i is visiting and move i forward. At this point, we found the maximum size of substrings without duplicate characters start with index i. We move pointer one step further.

When j pointer iterates all the characters of the string, we get the max length of the longest substring without repeating characters.

## Java Solution

class Solution {
public int lengthOfLongestSubstring(String s) {
int maxLength = 0;
HashSet<Character> set = new HashSet<>();

int i = 0;
int j = 0;
while (j < s.length()) {
if (!set.contains(s.charAt(j))) {
j++;
maxLength = Math.max(maxLength, j - i);
} else {
set.remove(s.charAt(i));
i++;
}
}
return maxLength;
}
}

## Python Solution

class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:

counter = {}

j = 0

longest = 0

for i in range(len(s)):
while j < len(s) and s[j] not in counter:
longest = max(longest, j - i + 1)
counter[s[j]] = counter.get(s[j], 0) + 1
j += 1

counter[s[i]] -= 1

if counter[s[i]] == 0:
del counter[s[i]]

return longest
• Time complexity: O(n).
• Space complexity: O(m). m is the size of the charset.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7382744,"math_prob":0.9534461,"size":2278,"snap":"2022-05-2022-21","text_gpt3_token_len":592,"char_repetition_ratio":0.14379947,"word_repetition_ratio":0.05602241,"special_character_ratio":0.26777875,"punctuation_ratio":0.17456897,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9780472,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-24T07:01:01Z\",\"WARC-Record-ID\":\"<urn:uuid:0eb1dffa-0826-42ef-be70-d02d7833349b>\",\"Content-Length\":\"54519\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c5410e9c-4f55-4736-bf04-4ad02b986361>\",\"WARC-Concurrent-To\":\"<urn:uuid:fc597841-13e8-497c-87d0-9c3105cebaa6>\",\"WARC-IP-Address\":\"72.44.43.28\",\"WARC-Target-URI\":\"https://goodtecher.com/leetcode-3-longest-substring-without-repeating-characters/\",\"WARC-Payload-Digest\":\"sha1:KPHLITENBGZU4ERD7XOMV6R2AGFC5N7O\",\"WARC-Block-Digest\":\"sha1:KXYNO5WEROCXLPBQNAWDB4IN5YGZLAWJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662564830.55_warc_CC-MAIN-20220524045003-20220524075003-00245.warc.gz\"}"}