class Solution {

public:

int lengthOfLongestSubstring(string s) {

```
int n = s.length(),i=0;
int h[256],next[n],first[n],ans=0;
for(i=0; i<256; i++) h[i] = n;
first[n] = n;
for(i=n-1; i>=0; i--){
next[i] = h[ s[i] ];
h[s[i]] = i;
first[i] = first[i+1] > next[i] ? next[i] : first[i+1];
ans = ans > first[i]-i ? ans : first[i]-i ;
}
return ans;
}
```

};