3. Longest Substring Without Repeating Characters

PHOTO EMBED

Fri May 26 2023 07:10:39 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

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;
    }
};
content_copyCOPY

https://leetcode.com/problems/longest-substring-without-repeating-characters/