Skip to content

Commit 85365de

Browse files
authored
Update 0003-longest-substring-without-repeating-characters.cpp as in the video
Submission url: https://leetcode.com/problems/longest-substring-without-repeating-characters/submissions/872218190/ Problem: 0003-longest-substring-without-repeating-characters.cpp Language: C++
1 parent e369073 commit 85365de

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

cpp/0003-longest-substring-without-repeating-characters.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Time: O(n)
88
Space: O(n)
99
*/
10-
10+
/*
1111
class Solution {
1212
public:
1313
int lengthOfLongestSubstring(string s) {
@@ -32,3 +32,25 @@ class Solution {
3232
return result;
3333
}
3434
};
35+
*/
36+
// Same solution as above with the same amount of total iterations.
37+
// Above solution: no inner loop, but the "j" variable is not increased at each iteration
38+
// Below: inner loop increasing "i", outer loop increasing "j".
39+
class Solution {
40+
public:
41+
int lengthOfLongestSubstring(string& s) {
42+
unordered_set<char> chars;
43+
int maxSize = 0;
44+
int i = 0, j = 0;
45+
while (j < s.size()){
46+
while (chars.find(s[j]) != chars.end()){
47+
chars.erase(s[i]);
48+
++i;
49+
}
50+
maxSize = max(maxSize, j - i + 1);
51+
chars.insert(s[j]);
52+
++j;
53+
}
54+
return maxSize;
55+
}
56+
};

0 commit comments

Comments
 (0)