Longest Substring Without Repeating Characters
Given a string s
, find the length of the longest substring without repeating characters.
Example 1:
Example 2:
Example 3:
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Constraints:
s
consists of English letters, digits, symbols and spaces and can be null or empty.
Solutions
A substring is a contiguous sequence of characters within a string. You need to keep track of the maximum length of the substring found so far. This will be updated as we find longer substrings without repeating characters.
Approach – Brute Force Technique
Run two nested loop and keep track of the maximum length of the substring found so far. This will be updated as we find longer substrings without repeating characters.
Steps
Initialize maxLength: The variable
maxLength
is initialized to 0. This variable will keep track of the length of the longest substring without repeating characters that we've found so far.Check if string is not null: The algorithm checks if the string
s
is not null. Ifs
is null, the function will returnmaxLength
, which is 0.Iterate over the string: If
s
is not null, the algorithm iterates over each character in the string using afor
loop, withi
as the index.Create a HashSet of characters: For each character at index
i
, a new HashSetchars
is created and the character is added to it. A HashSet is a collection of unique elements.Check for repeating characters: The algorithm then iterates over the rest of the string starting from
i + 1
. If the current characters[j]
is already in the HashSetchars
, it means we've found a repeating character and we break out of the inner loop.Add non-repeating characters: If
s[j]
is not inchars
, it means it's a non-repeating character and it's added tochars
.Update maxLength: After considering each character
s[j]
, the algorithm checks if the number of characters inchars
(which represents the length of the current substring without repeating characters) is greater thanmaxLength
. If it is,maxLength
is updated to the current substring's length.Return maxLength: After the outer loop has finished executing,
maxLength
will hold the length of the longest substring without repeating characters ins
, and this value is returned.
Complexity
Time complexity:
O(N^2)
Space complexity:
O(N)
Approach – Dynamic Sliding Window Technique
This is a dynamic sliding window approach. The term "dynamic" refers to the fact that the window size isn't fixed - it adjusts dynamically based on the conditions of the problem, which in this case is the presence of repeating characters. When a repeating character is found, the window "slides" by moving its start position to the right. This dynamic adjustment of the window size is what makes it a dynamic sliding window approach.
Steps
Initialize Variables: The variables
n
,set
,ans
,i
, andj
are initialized.n
is the length of the strings
.set
is a HashSet that will store the characters in the current window[i, j)
.ans
will store the length of the longest substring without repeating characters.i
andj
are pointers representing the start and end of the sliding window, respectively.Start of Loop: The
while
loop will continue as long as bothi
andj
are less thann
, the length of the string.Check for Character in Set: For each iteration of the loop, the algorithm checks if the character at position
j
in the strings
is in the HashSetset
.Character Not in Set: If the character
s[j]
is not inset
, it is added toset
andj
is incremented by 1. Theans
variable is then updated to be the maximum ofans
and the difference betweenj
andi
, which represents the length of the current substring without repeating characters.Character in Set: If the character
s[j]
is inset
, it means we've found a repeating character and we need to move the start of the window to the right. So, the character at positioni
in the strings
is removed fromset
andi
is incremented by 1.Return Maximum Length: After the loop has finished executing,
ans
will hold the length of the longest substring without repeating characters ins
, and this value is returned.
Complexity
Time complexity:
O(N)
Space complexity:
O(N)
Last updated