Likou Brushing Hundred Days Plan Day2 The longest substring without repeated characters

learning target:

I will continue to update my unique algorithm ideas, hoping to bring you different thinking expansion!
If you find it helpful, please like, follow and support!
Your encouragement is what keeps me going!
! ! !

Likou Question Bank Question 3 Official Link


Learning Content:

longest substring without repeating characters

给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: Since the longest substring without repeating characters is "abc", its length is 3.
Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: Since the longest substring without repeating characters is "b", its length is 1.
Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: Since 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 subsequence, not a substring.
Example 4:

input: s = ""
output: 0

提示:

0 <= s.length <= 5 * 104
s 由英文字母、数字、符号和空格组成

study-time:

2022.1.8


Learning output:

insert image description here
Why does it take so long to execute?
Because we use the List collection to help us complete the implementation of some methods, it may save time for us to implement it ourselves.

Problem solving ideas

idea one

Base address longest unique field method

1. First of all, we need to understand the idea of ​​solving the problem, and use a base address to judge whether the following string meets the requirements of "non-repeating strings". First, put the base address in the List collection (the List collection helps us solve it here). For many calculations, if there is no List, we can define a function by ourselves to replace the function of List. For example, Add, Count, IndexOf, Clear)
2. Then try to put the characters after the base address one by one. Determine whether this character exists in the List collection
3. If it exists, repeat it, break directly, and compare the number of characters in the List with the maximum value. If it is greater than the maximum value, replace it with the maximum value. If the character does not exist in the List collection, it will be added and the next character will be judged.
4. After the base address is judged, we can get the longest non-repeating substring starting with this base address. Then, we move the base address backward, and then repeat the above operation.
5. We can get the longest non-repeating substring starting from each base address, and then return the maximum value of them.

public class Solution {
    
    
    public int LengthOfLongestSubstring(string s) {
    
    
            
        
            int Maxlength=0;
            List<char> temp=new List<char>();  //定义List集合
            for(int i=0;i<s.Length;i++){
    
    
                temp.Clear();
                temp.Add(s[i]);  //基地址加入集合
                //遍历后面的全部字符
                for(int j=i+1;j<s.Length;j++){
    
    
                    if(temp.IndexOf(s[j])==-1){
    
       //如果不存在就放入
                        temp.Add(s[j]);
                    }else{
    
    
                        break;  //如果存在就可以break
                    }
                }
                //遍历完成后将List集合长度与最大值做比较 更新最大值
                if(temp.Count()>Maxlength){
    
    
                 Maxlength=temp.Count();
                }
            }

            return Maxlength;

    }
}

idea two

sliding window

1. First of all, the name of the sliding window sounds like it has nothing to do with the title, but we can imagine that there is a window, which can be similar to the queue
2. First, start from index 0, and slowly open the window forward, that is, join the queue one by one, If the added element already exists in the window, put the new element into
3. Then move the queue to the right (shrink to the right) until all duplicate elements are removed
4. Then there is no duplicate in the queue now Substring, then continue to put in, repeat this process
5. In this process, compare with MaxLength each time, update the length of the maximum non-repeated string in real time.

diagram
insert image description here

public class Solution {
    
    
    public int LengthOfLongestSubstring(string s) {
    
    
            int Maxlength=0;
            int LeftIndex=0;
            List<char> temp=new List<char>();  //定义List集合
            for(int i=0;i<s.Length;i++){
    
    
                if(temp.IndexOf(s[i])!=-1){
    
       //如果存在
                    LeftIndex= Math.Max( temp.LastIndexOf(s[i])+1,LeftIndex);
                }
                temp.Add(s[i]);  
                //遍历完成后将List集合长度与最大值做比较 更新最大值
                if(temp.Count()-LeftIndex>=Maxlength){
    
    
                    Maxlength=temp.Count()-LeftIndex;
                }
            }

            return Maxlength;

    }
}

Author: guinea pig Xiaohuihui
The copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/m0_48781656/article/details/122388112