Answers for "longest substring without repeating characters"

C++
0

longest substring without repeating characters

#include <bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    cin>>s;
    int l=s.length();
    map<char,int>mp;
    int mx=INT_MIN;
    int i=0;
    int j=0;
    while(j<l)
    {
        mp[s[j]]++;
        if(int(mp.size())<j-i+1)
        {
          while(int(mp.size())<j-i+1)
          {
              mp.erase(s[i]);
              i++;
          }
          j++;
        }
        else if(int(mp.size())==j-i+1)
        {
            mx=max(mx,j-i+1);
            j++;
        }
    }
    cout<<mx;
    return 0;
}
Posted by: Guest on June-29-2021

Code answers related to "longest substring without repeating characters"

Browse Popular Code Answers by Language