-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.longest-substring-without-repeating-characters.cpp
More file actions
48 lines (45 loc) · 1.39 KB
/
3.longest-substring-without-repeating-characters.cpp
File metadata and controls
48 lines (45 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s.length() == 0)
return 0;
// set<char> char_set;
// char_set.insert(s[0]);
// int res_max=0;
// for(int i=1; i<s.length(); i++)
// {
// if(char_set.find(s[i]) == char_set.end())
// {
// char_set.insert(s[i]);
// res_max = max((int)char_set.size(), res_max);
// }
// else
// {
// char_set.erase(char_set.find(s[i]));
// // char_set.insert(s[i]);
// cout<<char_set.size();
// }
// }
// res_max = max((int)char_set.size(), res_max);
// cout<<char_set.size();
// char_set.clear();
// return res_max;
unordered_set<char> set;
int i = 0, j = 0, n = s.size(), ans = 0;
while( i<n && j<n)
{
if(set.find(s[j]) == set.end()) //If the character does not in the set
{
set.insert(s[j++]); //Insert the character in set and update the j counter
ans = max(ans, j-i); //Check if the new distance is longer than the current answer
}
else
{
set.erase(s[i++]);
/*If character does exist in the set, ie. it is a repeated character,
we update the left side counter i, and continue with the checking for substring. */
}
}
return ans;
}
};