# LeetCode3. Longest substring without repeating characters (C + +)

Given a string, please find out the length of the longest substring that does not contain duplicate characters.

Example 1:

Type: "abcabcbb"
Output: 3
Explanation: because the longest substring without repeating characters is "abc", its length is 3.
Example 2:

Enter: "bbbbb"
Output: 1
Explanation: because the longest substring without repeating characters is "b", its length is 1.
Example 3:

Enter: "pwwkew"
Output: 3
Explanation: because the longest substring without repeating characters is "wke", its length is 3.
Note that your answer must be the length of the substring, "pwke" is a substring, not a substring.

Source: LeetCode

//Slide the window. At this time, the string satisfying the condition is between the lower i and the subscript J

```class Solution {
public:
int lengthOfLongestSubstring(string s) {
int size=s.length();
int j=0;
int Max=0;
for(int i=0;i<size;i++){
for( int k=j;k<i;k++){
if(s[i]==s[k]){
j=k+1;
break;
}
}
Max=max(Max,i-j+1);

}
return Max;
}
};```

////Double pointers form a sliding window and construct a hash table for optimization

```class Solution {
public:
int lengthOfLongestSubstring(string s) {
bool size[95]={0};  //Play the role of hash to store the number of elements in the sliding window. 32-126 (95 in total) are characters (32 is space), among which 48-57 are ten Arabic numbers from 0 to 9.
int i=0;   //Front pointer
int j=0;   //Pointer after
int Max=0;
if(s.length()<2){  //When the string length is less than 2, there will be no repetition. Just return the length of the string
return s.length();
}
while(j<s.length()){
if(size[s[j]-' ']==0){    //When the j-th element is not in the sliding window
size[s[j]-' ']=1;     //Put the element with the subscript j into the sliding window, and mark it as 1, indicating existence
Max=max(j-i+1,Max);   //Record the length of this time
j++;                  //Put the element with the subscript j in the sliding window
}else{                    //When the j-th element already exists in the window
while( i<j){          //Move i back until the repeating element is removed
if(s[i]==s[j]){
i++;
j++;
break;
}
size[s[i]-' ']=0;
i++;
}
}
}
return Max;

}
};```

//It can be seen that 95 bools have been opened up to check whether they are in the sliding window, but it's a bit wasteful to use a bool to store space. So I have carried out the next step of optimization, using bits to store to save space

```//Two pointers form a sliding window
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int  size[3]={0};  //Play the role of hash to store whether there is an element in the sliding window. 32-126 (95 in total) are characters (32 is space). Among them, 48-57 are ten Arabic numbers from 0 to 9, and an int has 32 bytes, so three ints are needed to store.
int i=0;   //Front pointer
int j=0;   //Pointer after
int Max=0;
if(s.length()<2){  //When the string length is less than 2, there will be no repetition. Just return the length of the string
return s.length();
}
while(j<s.length()){
if((size[(s[j]-' ')/32]&(1<<((s[j]-' ')%32)))==0){    //When the j-th element is not in the sliding window
size[(s[j]-' ')/32]=(size[(s[j]-' ')/32]|(1<<((s[j]-' ')%32)));     //Put the element with the subscript j into the sliding window, and mark it as 1, indicating existence
Max=max(j-i+1,Max);   //Record the length of this time
j++;                  //Put the element with the subscript j in the sliding window
}else{                    //When the j-th element already exists in the window
while( i<j){          //Move i back until the repeating element is removed
if(s[i]==s[j]){
i++;
j++;
break;
}
size[(s[i]-' ')/32]=(size[(s[i]-' ')/32]&(~(1<<(s[i]-' ')%32)));
i++;
}
}
}
return Max;

}
};```

//Supplementary knowledge points:

//32-126 (95 in total) are characters (32 is space), of which 48-57 are ten Arabic numbers from 0 to 9.

//65-90 are 26 capital English letters, 97-122 are 26 small English letters, the rest are some punctuation marks, operation symbols, etc.

140 original articles published, 43 praised, 30000 visitors+

Tags: less network REST

Posted on Sun, 02 Feb 2020 07:54:46 -0500 by jdavidbakr