-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestSubstringWithoutRepeatingCharacters.cpp
More file actions
39 lines (35 loc) · 1.07 KB
/
LongestSubstringWithoutRepeatingCharacters.cpp
File metadata and controls
39 lines (35 loc) · 1.07 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
#include <iostream>
#include <set>
using namespace std;
class Solution {
public:
// A substring is a continuous, smaller sequence of characters taken from a larger string
int lengthOfLongestSubstring(string s) {
int maxLen = 0;
int currentLength=0;
set<char> tracked_chars;
// char tracker
for(size_t i =0; i < s.size(); ++i){
tracked_chars.clear();
for(size_t j =i; j < s.size(); ++j){
if(tracked_chars.count(s[j])){
break;
}
tracked_chars.insert(s[j]);
currentLength = j - i + 1 ;
maxLen = max(maxLen, currentLength);
}
}
return maxLen;
}
};
int main(){
string Input = "abcabcbb";
string Input2 = "bbbbbb";
string Input3 = "";
Solution sol;
cout << sol.lengthOfLongestSubstring(Input)<< endl; // should be 3
cout << sol.lengthOfLongestSubstring(Input2) << endl; // should be 1
cout << sol.lengthOfLongestSubstring(Input3) << endl; // should be 0
return 0;
}