The problem can be found at the following link: Problem Link
Given a string s
, your task is to find the length of the longest substring with all distinct characters.
Input:
s = "geeksforgeeks"
Output:
7
Explanation:
"eksforg" is the longest substring with all distinct characters.
Input:
s = "aaa"
Output:
1
Explanation:
The longest substring with all distinct characters is "a".
Input:
s = "abcdefabcbb"
Output:
6
Explanation:
The longest substring with all distinct characters is "abcdef", which has a length of 6.
$1 <= s.size() <= 3 * 10^4$ - All characters are lowercase English alphabets.
-
Sliding Window Technique:
To find the longest substring with distinct characters, we maintain a sliding window that contains only unique characters. If a duplicate character is found, we slide the window forward to remove it. -
Steps:
- Use an array
lastSeen
to store the last seen index of each character. - Traverse the string using a pointer (
end
) to extend the window. - Use a second pointer (
start
) to mark the beginning of the valid window. If a duplicate is found, movestart
to ensure all characters in the window are unique. - Keep track of the maximum length of the substring during the traversal.
- Use an array
-
Edge Cases:
- Empty or very small strings.
- Strings with all identical characters.
- Strings with all unique characters.
- Expected Time Complexity: O(n), where
n
is the length of the string. Each character is processed at most twice (once when extending the window and once when shrinking it). - Expected Auxiliary Space Complexity: O(1), as the space used for the
lastSeen
array is constant (128 elements for ASCII).
class Solution {
public:
int longestUniqueSubstr(string &s) {
int lastSeen[128] = {}, maxLength = 0, start = 0;
for (int end = 0; end < s.size(); ++end) {
start = max(start, lastSeen[s[end]]);
lastSeen[s[end]] = end + 1;
maxLength = max(maxLength, end - start + 1);
}
return maxLength;
}
};
class Solution {
public int longestUniqueSubstr(String s) {
int[] lastSeen = new int[128];
int maxLength = 0, start = 0;
for (int end = 0; end < s.length(); end++) {
start = Math.max(start, lastSeen[s.charAt(end)]);
lastSeen[s.charAt(end)] = end + 1;
maxLength = Math.max(maxLength, end - start + 1);
}
return maxLength;
}
}
class Solution:
def longestUniqueSubstr(self, s):
last_seen = [-1] * 128
max_length = 0
start = 0
for end, char in enumerate(s):
start = max(start, last_seen[ord(char)] + 1)
last_seen[ord(char)] = end
max_length = max(max_length, end - start + 1)
return max_length
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐