Skip to content

Commit 968efdc

Browse files
authored
Merge pull request #477 from nidhisethi88/master
Added solution to leetcode 3 in cpp
2 parents f3f7a77 + bd2f073 commit 968efdc

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
int lengthOfLongestSubstring(string s) {
2+
if(s.length()<=1)
3+
return s.length();
4+
vector<int> freq(128,0);
5+
int si=0,ei=0,n=s.length(),maxlen=INT_MIN,count=0;
6+
while(ei<n){
7+
if(freq[s[ei++]]++ > 0)
8+
count++;
9+
10+
while(count>0){ //if freq of char at ei in the chosen substring is greater than 2
11+
if(freq[s[si++]]-- >1) //iterate till the freq becomes 1
12+
count--;
13+
}
14+
15+
if(ei-si>maxlen) //each time compare length of obtained substr with unique char to the maximum length
16+
maxlen=ei-si;
17+
}
18+
return maxlen;
19+
}

0 commit comments

Comments
 (0)