Let’s chew the algorithm together — the longest substring of non repeated characters

Time:2020-10-22

Longest substring without repeating characters

This isLeetCodeThe third question is as follows:

Given a string, please find out the length of the longest substring without repeating characters.
Example 1:
Input: “abcabcbbb”
Output: 3
Explanation: since the longest substring of an unrepeated character is “ABC”, its length is 3.
Example 2:
Input: “bbbbbbb”
Output: 1
Explanation: since the longest substring of an unrepeated character is “B”, its length is 1.
Example 3:
Input: “pwwkew”
Output: 3
Explanation: since the longest substring of a non repeating character is “WKE”, its length is 3. Please note that your answer must be the length of the substring, “pwke” is a subsequence, not a substring.
Source: Buckle

Thinking of problem solving

Conventional thinking: two levels of loop strings, and we need oneMap dictionaryTo mark which characters have appeared, the process is as follows:
Let's chew the algorithm together -- the longest substring of non repeated characters

Code implementation of go language:

func lengthOfLongestSubstring(s string) (length int) {
    if "" == s {
        return
    }
    length = 1
    for i := 0; i < len(s); i++ {
        //The ASCII range of characters is 0-127, so the key type of map is uint8
        dict := make(map[uint8]bool)
        dict[s[i]] = true
        for j := i + 1; j < len(s); j++ {
            //Determine whether the character is in the map, and if it exists, it will jump out of the loop directly
            if _, ok := dict[s[j]]; ok {
                break
            }
            //Put the character in the map
            dict[s[j]] = true
            //The maximum is calculated each time
            cMax := j - i + 1
            if cMax > length {
                length = cMax
            }
        }
    }
    return
}

In fact, the main test point of this topic lies in the rightsliding windowUnderstanding.

Slide window:Sliding window is an abstract concept commonly used in array and string problems.
A window is usually a set of elements defined by the start and end indexes in an array or string, i.e. [I, J) (left closed, right open).
Sliding window is a window that can “slide” two boundaries in a certain direction. For example, we slide [I, J) one element to the right,
Then it becomes [i + 1, j + 1) [i + 1, j + 1) (left closed, right open).

Then borrowsliding windowHow to solve the problem? I have drawn a sketch here, in which[X,Y)namelysliding window
Let's chew the algorithm together -- the longest substring of non repeated characters
here you areA note: whenYPoint to the character in[X,Y)When sliding between windows, the index value is assumed to beindexAt this timeXIt’s moving directly toindex+1The location of.

Code implementation of go language:

func lengthOfLongestSubstring(s string) int {
    var Length int
    var s1 string
    x := 0
    y := 0
    s1 = s[x:y]
    for ; y < len(s); y++ {
        // strings.IndexByte  It is used to determine whether the character corresponding to y is in [x, y]. If it does not exist, it returns - 1
        if index := strings.IndexByte(s1, s[y]); index != -1 {
            //If it exists, X jumps directly to the next digit of the index
            x += index + 1
        }
        s1 = s[x : y+1]
        if len(s1) > Length {
            Length = len(s1)
        }
    }
    return Length
}

summary

Make progress every day, come on!
Algorithm tutorial project, update a question every day, click a star to support it
https://github.com/wx-satellite/learning-algorithm

Recommended Today

Comparison and analysis of Py = > redis and python operation redis syntax

preface R: For redis cli P: Redis for Python get ready pip install redis pool = redis.ConnectionPool(host=’39.107.86.223′, port=6379, db=1) redis = redis.Redis(connection_pool=pool) Redis. All commands I have omitted all the following commands. If there are conflicts with Python built-in functions, I will add redis Global command Dbsize (number of returned keys) R: dbsize P: print(redis.dbsize()) […]