Given a string, find the length of the longest substring T that contains at most 2 distinct characters.
For example, Given s =
“eceba”
,
T is "ece" which its length is 3.
Solution:
To solve substring problem, could use following template:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int findSubstring(string s){ | |
vector<int> map(128,0); | |
int counter; // check whether the substring is valid | |
int begin=0, end=0; //two pointers, one point to tail and one head | |
int d; //the length of substring | |
for() { /* initialize the hash map here */ } | |
while(end<s.size()){ | |
if(map[s[end++]]-- ?){ /* modify counter here */ } | |
while(/* counter condition */){ | |
/* update d here if finding minimum*/ | |
//increase begin to make it invalid/valid again | |
if(map[s[begin++]]++ ?){ /*modify counter here*/ } | |
} | |
/* update d here if finding maximum*/ | |
} | |
return d; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int lengthOfLongestSubstringTwoDistinct(string s) { | |
vector<int> map(128, 0); | |
int counter=0; | |
int start=0, end=0; | |
int d=0; | |
while(end<s.size()){ | |
if(map[s[end]]!=0){ | |
map[s[end]]+=1; | |
end++; | |
continue; | |
} | |
if(map[s[end]]==0){ | |
if(counter<2){ | |
map[s[end]]+=1; | |
counter++; | |
end++; | |
continue; | |
} | |
if(counter==2){ | |
d=end-start>d? end-start:d; | |
while(counter==2){ | |
map[s[start]]-=1; | |
if(map[s[start]]==0) | |
counter--; | |
start++; | |
} | |
} | |
} | |
} | |
return max(d, end-start); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int lengthOfLongestSubstringTwoDistinct(string s) { | |
vector<int> map(128, 0); | |
int counter=0, begin=0, end=0, d=0; | |
while(end<s.size()){ | |
if(map[s[end++]]++==0) counter++; | |
while(counter>2) if(map[s[begin++]]--==1) counter--; | |
d=max(d, end-begin); | |
} | |
return d; | |
} |
Round 2 solution:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int lengthOfLongestSubstringTwoDistinct(string s) { | |
int res=0; | |
if(s.length()==0) | |
return res; | |
int p0=0, p1=0; | |
vector<int> myHash(128, 0); | |
int count=0; | |
while(p1<s.length()){ | |
char c = s[p1]; | |
if(myHash[c]==0) | |
count++; | |
myHash[c]++; | |
if(count>2){ | |
while(p0<s.length()){ | |
if(--myHash[s[p0++]]==0) | |
break; | |
} | |
count--; | |
}else | |
res = max(res, p1-p0+1); | |
p1++; | |
} | |
return res; | |
} | |
}; |
Rnd3 sol:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int lengthOfLongestSubstringTwoDistinct(string s) { | |
int res=0; | |
if(s.empty()) | |
return res; | |
unordered_map<char, int> myMap; | |
int p0=0, p1=0; | |
int count = 0; | |
while(p1<s.length()){ | |
char c = s[p1]; | |
myMap[c]++; | |
count = myMap[c]==1? count+1:count; | |
if(count>2){ | |
while(p0<s.length()-1&&--myMap[s[p0++]]!=0){} | |
count--; | |
} | |
res = max(res, p1-p0+1); | |
p1++; | |
} | |
return res; | |
} |
No comments:
Post a Comment