**Code first**

```
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if (s.length() == 0) return 0;
string str=s.substr(0,1); //current longest substring
int max = str.length(); // find the max length
for (int i = 1; i < s.length(); i++){
int pos = str.find_first_of(s[i]); //return position if s[i] in str or -1 otherwise
str += s.substr(i,1);
if (pos>=0)
str = str.substr(pos+1, str.length()-pos);
if (max < str.length())
max = str.length();
}
return max;
}
};
```

My opinion, the **time complexity rely on the input case**. O(n) or O(n*n).

**Best case** The elements in string are **same**, such that O(n) is enough.

**Worst case** The characters in string are all **unique**, such that we need compare all characters before.

Thus, the time complexity is O(n) orO(nlgn) using map that implement with binary tree.

**Finally**, I'd like to say that how **lucky** you are who deal with the ascii character only.

When it refer to Unicode string, you can not play int[256] trick or something like that again.

I mean tens of thousand space can not be overlooked.

**Forgive me** if I didn't make myself clear, and questions are welcome