Given a string, find the length of the longest substring without repeating characters.
Input: A single string s
.
Output: An integer representing the length of the longest substring without repeating characters.
Constraints:
0 ≤ s.length ≤ 5 * 104
s
consists of English letters, digits, symbols, and spaces.Example:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
The core challenge of this problem is to find the longest substring in a given string that does not contain any repeating characters. This problem is significant in various applications such as data compression, pattern recognition, and text processing. A common pitfall is to overlook the need for an efficient solution, especially given the constraint that the string length can be up to 50,000 characters.
To solve this problem, we need to consider different approaches:
The naive solution involves checking all possible substrings and determining if they contain repeating characters. This approach is not optimal due to its high time complexity of O(n3).
A more efficient approach is to use the sliding window technique with a hash set to keep track of characters in the current window. This allows us to achieve a linear time complexity of O(n).
Here is a step-by-step breakdown of the optimized algorithm:
left
and right
, both set to the start of the string.right
pointer.right
is not in the hash set, add it to the set and update the maximum length.right
is already in the hash set, remove characters from the set starting from left
until the duplicate character is removed.right
pointer reaches the end of the string.public class Solution {
public int lengthOfLongestSubstring(String s) {
// HashSet to store the characters in the current window
Set<Character> set = new HashSet<>();
int left = 0, right = 0, maxLength = 0;
// Iterate through the string with the right pointer
while (right < s.length()) {
// If the character at right is not in the set, add it
if (!set.contains(s.charAt(right))) {
set.add(s.charAt(right));
right++;
maxLength = Math.max(maxLength, right - left);
} else {
// If the character at right is in the set, remove the left character
set.remove(s.charAt(left));
left++;
}
}
return maxLength;
}
}
The time complexity of the optimized solution is O(n) because each character is processed at most twice (once by the right pointer and once by the left pointer). The space complexity is O(min(n, m)), where n is the length of the string and m is the size of the character set.
Potential edge cases include:
Examples:
Input: s = ""
Output: 0
Input: s = "aaaaa"
Output: 1
Input: s = "abcdef"
Output: 6
To test the solution comprehensively, consider the following test cases:
Use testing frameworks such as JUnit to automate the testing process.
When approaching such problems, consider the following tips:
In this blog post, we discussed how to find the longest substring without repeating characters using an optimized approach with a time complexity of O(n). Understanding and solving such problems is crucial for improving your algorithmic skills and preparing for coding interviews.
For further reading and practice, consider the following resources: