-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestsubstrring.cpp
More file actions
48 lines (41 loc) · 931 Bytes
/
longestsubstrring.cpp
File metadata and controls
48 lines (41 loc) · 931 Bytes
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
//Find the length of the longest substring without repeating characters.
#include <iostream>
#include <set>
using namespace std;
class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
int i = 0, j = 0;
int max_len = 0;
set<char>substring;
while(j < s.length())
{
if(substring.find(s[j])==substring.end())
{
substring.insert(s[j]);
// cout<<s[j]<<endl;
max_len = max(max_len,j-i+1);
j++;
}
else
{
// cout<<s[i]<<endl;
substring.erase(s[i]);
// j++;
i++;
}
}
return max_len;
}
};
int main()
{
Solution s;
string str1;
cout << "Enter a string: ";
getline(cin,str1);
cout<<s.lengthOfLongestSubstring(str1);
return 0;
}