Longest Substring with at the most of the Distinct characters
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 it length is 3.
This problem is obviously traversed using a double pointer, and the window between Begin and end represents the string of characters that meet the requirements.
Solution One:
The Inwindow array saves two different characters in the window. (if two different characters are not found, return the string length directly)
Map<char, Int> saves the number of occurrences of two characters in a window in a inwindow.
The criterion for judging the new character is not equal to any of the characters of the Inwindow array, then you need to shrink the begin until Inwindow frees up space for the new character.
classSolution { Public: intLengthoflongestsubstringtwodistinct (strings) {if(s = ="") return 0; Else if(S.size () <=2) returns.size (); //slip window [begin, end]//Initial the window with the different chars intSize =s.size (); intBegin =0; intEnd =1; while(End < size && S[end] = =S[begin]) End++; //to here, end = = Size or S[end]! = S[begin] if(End = =size)returnSize//All chars is the same Charinwindow[2] ={S[begin], s[end]}; Map<Char,int> m;//Char->count MapM[s[begin]] = End-begin;//[Begin,end) is all S[begin]M[s[end]] =1; intLongest = end-begin+1; End++; while(End <size) {M[s[end]]++; if(S[end] = = inwindow[0] || S[end] = = inwindow[1]) //in window, Extend endLongest = max (longest, end-begin+1); Else {//Not in window, shrink begin//remove a char from window while(m[inwindow[0]] !=0&& m[inwindow[1]] !=0) {M[s[begin]]--; Begin++; } //to here, either m[inwindow[0]] = = 0 or m[inwindow[1]] = = 0 if(m[inwindow[0]] ==0) inwindow[0] =S[end]; Elseinwindow[1] =S[end]; } End++; } returnlongest; }};
Solution Two:
Since the ASCII code of A~Z,A~Z does not exceed 122, it opens up 128 of the array record for each character count in the window.
Set the count of counts again to record how many different characters are in the current window.
The standard for judging new characters is a record value of 1 (after adding new characters).
If count>2, then it is necessary to shrink the begin until S[begin] corresponds to a count of 0, which represents a less-than-a-class character, count--
classSolution { Public: intLengthoflongestsubstringtwodistinct (strings) {if(S.size () <=2) returns.size (); intSize =s.size (); intrecord[ -] = {0};//record the appearance times of each char. Note ' Z ' is 122, and is enough. intBegin =0; intEnd =0; intCount =0;//Distinct Count intLongest =0; while(End <size) {Record[s[end]]++; if(Record[s[end]] = =1) //New CharCount + +; while(Count >2) {//ShrinkRecord[s[begin]]--; if(Record[s[begin]] = =0) Count--; //Remove one charBegin + +; } Longest= Max (Longest, end-begin+1); End++; } returnlongest; }};
I wrote the test with the following example, the two solution codes are all passed.
stringSTR1 ="";//expect:0 ""stringSTR2 ="a";//expect:1 "a"stringSTR3 ="AA";//expect:2 "AA"stringSTR4 ="ABA";//expect:3 "ABA"stringSTR5 ="ABCD";//expect:2 "AB"stringSTR6 ="ABCDEDCBA";//expect:3 "Ded"stringSTR7 ="ABBCDEDEDCBA";//expect:5 "deded"stringStr8 ="Eceba";//expect:3 "ECE"stringSTR9 ="abaece";//expect:3 "ABA"stringStr10 ="ABABCD";//expect:4 "Abab"stringStr11 ="CABABCD";//expect:4 "Abab"stringStr12 ="ABCDEFGABCDEFG";//expect:2 "AB"stringStr13 ="Ababababababab";//expect:14 "Ababababababab"
"Leetcode" longest Substring with at most of the Distinct characters (2 solutions)