-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno_repeated_substring.cpp
More file actions
45 lines (40 loc) · 1.25 KB
/
no_repeated_substring.cpp
File metadata and controls
45 lines (40 loc) · 1.25 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
/*
Given a string, find the length of the longest substring which has no repeating characters.
*/
#include<bits/stdc++.h>
using namespace std;
class Solution {
private:
string input;
public:
void getInput() {
cin>>input;
}
int maxUniqueString() {
unordered_map<char, int> lastIndex;
int start=0, ans = INT_MIN, sIndex = -1,eIndex = -1;
for(int end=0; end<input.length(); end++) {
// if current charater is already present in string
if(lastIndex.find(input[end]) != lastIndex.end()) {
//set start properly
start = lastIndex[input[end]]+1;
}
if(ans <= end-start+1) {
ans = end-start+1;
sIndex = start;
eIndex = end;
}
lastIndex[input[end]] = end;
}
cout<<"startIndex: "<<sIndex<<endl<<"lastIndex: "<<eIndex<<endl;
return ans;
}
};
int main() {
Solution currObj;
currObj.getInput();
cout<<currObj.maxUniqueString()<<endl;
currObj.getInput();
cout<<currObj.maxUniqueString()<<endl;
return 0;
}