O(N) runtime , constant space solution.
\nKeep track of the last index of each char. Scan s from left to right, if hit a previously encountered char, then reset the longest substring to start just aft
\nKeep track of the last index of each char. Scan s from left to right, if hit a previously encountered char, then reset the longest substring to start just after the last index of the seen char.\n\n        int lengthOfLongestSubstring(const string& s) {\n            int idx[256]; // indices of each char.\n            memset(idx,-1,sizeof idx);\n            int i,j,best=0,start=0;\n            for(i=0;i<s.size();i++){\n                const unsigned char c=(unsigned char)s[i];\n                j=idx[c];\n                idx[c]=i;\n                if(start<=j){\n                    start=j+1;\n                }\n                best=max(best,i-start+1);\n            }\n            return best;\n        }
[]