Skip to content

Commit dc57ec4

Browse files
authored
Merge branch 'main' into main
2 parents 7abadf5 + 512aa15 commit dc57ec4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <string>
2+
#include <unordered_set>
3+
#include <algorithm>
4+
5+
class Solution {
6+
public:
7+
int lengthOfLongestSubstring(std::string s) {
8+
int n = s.size();
9+
if (n == 0) {
10+
return 0;
11+
}
12+
std::unordered_set<char> char_set;
13+
int left = 0;
14+
int maxLength = 0;
15+
for (int right = 0; right < n; ++right) {
16+
char current_char = s[right];
17+
while (char_set.count(current_char)) {
18+
char_set.erase(s[left]);
19+
left++;
20+
}
21+
char_set.insert(current_char);
22+
maxLength = std::max(maxLength, right - left + 1);
23+
}
24+
25+
return maxLength;
26+
}
27+
};

0 commit comments

Comments
 (0)