-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram15.cpp
More file actions
21 lines (18 loc) · 778 Bytes
/
Copy pathprogram15.cpp
File metadata and controls
21 lines (18 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_set<char> charSet; // To store unique characters
int maxLength = 0; // To track the maximum length
int start = 0; // Sliding window start pointer
for (int end = 0; end < s.length(); end++) {
// If character at 'end' is already in the set, shrink the window
while (charSet.find(s[end]) != charSet.end()) {
charSet.erase(s[start]); // Remove the character at 'start'
start++; // Move the start pointer forward
}
charSet.insert(s[end]); // Add the current character to the set
maxLength = max(maxLength, end - start + 1); // Update maxLength
}
return maxLength;
}
};