Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Longest Substring Without Repeating Characters.cpp #202

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create Longest Substring Without Repeating Characters.cpp
kunalkabra46 authored Oct 10, 2022
commit f0c5eff18e6be307b410795f0b1a1ffdc97da147
33 changes: 33 additions & 0 deletions sliding-window/Longest Substring Without Repeating Characters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Question link: https://leetcode.com/problems/longest-substring-without-repeating-characters

class
{
public:
int lengthOfLongestSubstring(string s)
{
int i=0;
int j=0,mx=0;
unordered_map<char,int>mp;
while(j<s.size())
{
mp[s[j]]++;
if(mp.size()==j-i+1)
{
mx=max(mx,j-i+1);
j++;
}
else if(mp.size()<j-i+1)
{
while(mp.size()<j-i+1)
{
mp[s[i]]--;
if(mp[s[i]]==0)
mp.erase(s[i]);
i++;
}
j++;
}
}
return mx;
}
};