Leetcode Longest Substring Without Repeating Characters problem solution in C++ programming

In the Leetcode Longest Substring Without Repeating Characters problem solution in C++ programming Given a string s, find the length of the longest substring without repeating characters.

Leetcode Longest Substring Without Repeating Characters problem solution in C++ programming

class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char,int>mop;
int c=0, mx=0,p=1;
for(int i=0;i<s.size();i++)
{
if(mop[s[i]]&&p<=mop[s[i]])
{
c=(i+1)-(mop[s[i]]);
p=mop[s[i]]+1;
mop[s[i]]=i+1;
continue;
}
c++;
mop[s[i]]=i+1;
mx=max(c,mx);
}
return mx;
}
};

Also read,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *