Preview:
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int n=s.size();
        int cnt[256]={0};
        int i=0, j=0, ans=0;
        while(j<n)
        {
            cnt[s[j]]++;
            while(cnt[s[j]]>1)
            {
                cnt[s[i]]--;
                i++;
                
            }
            ans=max(ans, j-i+1);
            j++;
        }
        return ans;
    }
};
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter