O(N) TIME AND SPACE

class Solution {
public:
int lengthOfLongestSubstring(string s){
vectormp(256,-1);//set all 256 to -1;
int left=0;int right=0;
int n=s.size();
int len=0;
while(right<n){
if(mp[s[right]] !=-1) left=max(mp[s[right]] +1,left);////int map element are prestent that time this condition true
mp[s[right]]=right;//map is empty that time this condition true
len=max(len,right-left+1);
right++;
}
return len;
}

Comments (1)