Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize the longest substring algorithm of unrepeated characters in big data

Shulou Source: shulou.com Published: 2022-06-01 17:59:25 10月20日 Update

This article is about how to implement the longest substring algorithm without repeating characters in big data. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The longest substring without repeating characters

Train of thought answer

First define a pointer p to the chain head of the string, and then obtain the maximum length by whether the I character after the p pointer is the same as the j character after the p pointer. (also known as complete ergodic)

However, we are faced with a problem: if a string is particularly large, it will all be traversed, and the time complexity is O (n ^ 3). From this point of view, although it can solve the problem, it is too slow, and if the algorithm is not efficient, it will not make much sense.

Therefore, the following measures have been taken:

For example, to illustrate this measure, the string s is "dabcabcbb", when j points to the second "a" and I points to the first "a" when it is executed to j = 4, iDefin1. At this point, according to the previous traversal, the p pointer should be + 1 on ok, but if + 1, abca is a subset of dabca, so there is no need to judge this step, that is, you can omit this step! So the following optimizations are made:

Full traversal:

P = p + 1

After improvement:

P = p + I + 1

The complete code is as follows:

Int lengthOfLongestSubstring (char* s) {

Char* p = s

Int I = 0, j = 0, len = 0

For (j = 0; * (pairj)! ='\ 0mm; jacks +) {

For (I = 0; I)

< j; i++){ if(*(p+i) == *(p+j)){ p = p + i + 1; if(j >

Len) len = j

J = 0

Break

}

}

}

Len = (j > len)? J: len

Return len

}

Thank you for reading! This is the end of the article on "how to achieve the longest substring algorithm without repeating characters in big data". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!

Tags: Characters pointers longest algorithms strings points data content measures more articles questions good complex practical maximum same necessary that is code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Information Redmi Xiaomi Docker