```
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_set<char> table;
int tmpCnt = 0;
int maxCnt = 0;
for(int i = 0; i < s.length(); i++){
auto iter = table.find(s[i]);
if(iter == table.end()){
tmpCnt++;
}
else{
table.clear();
tmpCnt = 1;
int j = i-1;
while(s[j]!=s[i]){
table.emplace(s[j]);
j--; tmpCnt++;
}
}
table.emplace(s[i]);
if(tmpCnt > maxCnt) maxCnt = tmpCnt;
}
return maxCnt;
}
};
```

I tried to use a Unordered_set to keep track of the previous char I encountered. When I encounter a char that has previously appeared, I will firstly clear the content of the unordered_set, then push all the char behind the char that appeared twice back into the unordered_set. At the meantime, I am storing my counting result to a counter variable and finally use this variable to update my 'max' value.

But I am feeling terrible about this O(n^2) method. It would be great if you guys can give me some inspiration on a O(n) method.