Longest Substring Without Repeating Characters
Given a string s, find the length of the longest substring without repeating characters.
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Constraints:
sconsists 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
maxLengthis 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
sis not null. Ifsis null, the function will returnmaxLength, which is 0.Iterate over the string: If
sis not null, the algorithm iterates over each character in the string using aforloop, withias the index.Create a HashSet of characters: For each character at index
i, a new HashSetcharsis 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,maxLengthis updated to the current substring's length.Return maxLength: After the outer loop has finished executing,
maxLengthwill hold the length of the longest substring without repeating characters ins, and this value is returned.
public class Solution
{
public int LengthOfLongestSubstring(string s)
{
int maxLength = 0;
if (s is not null)
{
for (int i = 0; i < s.Length; i++)
{
HashSet<char> chars = new HashSet<char>();
chars.Add(s[i]);
for (int j = i + 1; j < s.Length; j++)
{
if (chars.Contains(s[j]))
{
break;
}
chars.Add(s[j]);
}
if (chars.Count > maxLength)
{
maxLength = chars.Count;
}
}
}
return maxLength;
}
}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, andjare initialized.nis the length of the strings.setis a HashSet that will store the characters in the current window[i, j).answill store the length of the longest substring without repeating characters.iandjare pointers representing the start and end of the sliding window, respectively.Start of Loop: The
whileloop will continue as long as bothiandjare 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
jin the stringsis in the HashSetset.Character Not in Set: If the character
s[j]is not inset, it is added tosetandjis incremented by 1. Theansvariable is then updated to be the maximum ofansand the difference betweenjandi, 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 positioniin the stringsis removed fromsetandiis incremented by 1.Return Maximum Length: After the loop has finished executing,
answill hold the length of the longest substring without repeating characters ins, and this value is returned.
public class Solution
{
public int LengthOfLongestSubstring(string s)
{
int n = s?.Length ?? -1;
HashSet<char> set = new HashSet<char>();
int ans = 0, i = 0, j = 0;
while (i < n && j < n)
{
// Try to extend the range [i, j]
if (!set.Contains(s[j]))
{
// add character into hashset and increment right counter
set.Add(s[j++]);
ans = Math.Max(ans, j - i);
}
else
{
// remove character from hashset and increment left counter
set.Remove(s[i++]);
}
}
return ans;
}
}Complexity
Time complexity:
O(N)Space complexity:
O(N)
Last updated