<p>Given a string <code>s</code>, find the length of the <strong>longest</strong> <span data-keyword="substring-nonempty"><strong>substring</strong></span> without repeating characters.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abcabcbb&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> The answer is &quot;abc&quot;, with the length of 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;bbbbb&quot;
<strong>Output:</strong> 1
<strong>Explanation:</strong> The answer is &quot;b&quot;, with the length of 1.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;pwwkew&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> The answer is &quot;wke&quot;, with the length of 3.
Notice that the answer must be a substring, &quot;pwke&quot; is a subsequence and not a substring.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>s</code> consists of English letters, digits, symbols and spaces.</li>
</ul>

class Solution {
public:
    bool check(string s, int l){
        map<char, int> mp;
        int n = s.length();
        for(int i =0; i<n; i++){
            if(i < l){
                mp[s[i]]++;
            }
            else{
                if(mp.size() == l)return true;
                mp[s[i-l]]--;
                if(mp[s[i-l]] == 0)mp.erase(s[i-l]);
                mp[s[i]]++;
            }
        }
        if(mp.size() == l)return true;
        return false;
    }

    int lengthOfLongestSubstring(string s) {
    //     set<char> st;
    //     int n = s.size();
    //     int i = 0;
    //     int j = 0;
    //     int ans = 0;
    //     while(j<n ){
    //         cout<<endl;
    //         while(st.size() == j-i){
    //             st.insert(s[j]);
    //             cout<<s[j]<<" ";
    //             j++;
    //         }
    //         int cur = st.size();
    //         ans = max(ans, cur);
    //         while(i < j-1 && s[i] != s[j-1]){
    //             st.erase(s[i]);
    //             i++;
    //         }
    //         st.insert(s[j]);
    //         j++;
    //     }
    //     return ans;

        int low = 0;
        int high = s.length();
        int ans = 0;
        while(low < high){
            int mid = (low + high)/2;
            if(low == mid)break;
            if(check(s, mid)){
                ans = max(ans, mid);
                low = mid+1;
            }
            else{
                high = mid-1;
            }
        }
        if(check(s, low))ans = max(ans, low);
        if(check(s, high))ans = max(ans, high);
        return ans;
    }
};
27091