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,
- Leetcode Longest Substring Without Repeating Characters problem solution in C
- Leetcode Longest Substring Without Repeating Characters problem solution in Java
- Leetcode Longest Substring Without Repeating Characters problem solution in Python
- Leetcode Longest Substring Without Repeating Characters problem solution in C#