#3 longest Substring without repeating characters

Source: Internet
Author: User
Tags repetition truncated

First, the topic

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "ABCABCBB" are "abc", which the length is 3. For "bbbbb" the longest substring are "B", with the length of 1.

Second, the analysis

To find the length of the maximum number of distinct sub-sequences, simply recognize the fact that if the new character repeats with a character in the existing self-sequence, it is not available until the repeating character is serialized, including itself, and only after it is retained.

For example, ABCBE, where B is duplicated, so that the string consisting of the first B (including itself) is duplicated, such as ABCB, BCB. So to delete AB, keep the CBE and go down.

Third, the realization of ideas

1, the implementation of string +hash, through the implementation of strings to parse the steps. The longest string currently obtained is stored in the hash table. 144ms

2, by string implementation, through the implementation of the process of parsing steps, with MaxLen record the longest string length. 104ms

3, with hash implementation, through the hash operation to achieve the resolution of the steps, while preserving the string to assist. 500ms

Four, difficulties

The difficulty of the string, it seems there is no difficulty after. The main idea is to think about it, the beginning of a little deviation, the repetition of characters into two cases: 1. The first character in the current string is the same, and a queue-like method is used to extrude the number one. 2. The same character as the current string, the current string is truncated before, starting from the repetition: such as DVDF, truncated after the DF. But this is obviously wrong, the longest should be VDF.

After discussing with the classmates, I found that it was true that there were only two cases, but the wrong way to deal with the problem in the 2nd was not comprehensive enough and the truncation was not correct. As a result, it is possible to include all cases in the parsing.

V. Python code (three versions)

1, List+hash

classSolution: # @param {string} s # @return {integer} _dict={} def createhashtable (self, longestsub):Global_dict Value=longestsub Key=len (value) isexist= _dict.Get(Key,'No')        ifIsexist = ='No': _dict[key]=valueElse: Pass def lengthoflongestsubstring (self, s):Global_dict longestsub=""length=Len (s) _dict= {}         forCurinchRange (length):
#检查当前要元素s [cur] is already in the current longest self-sequence longestsub
#如果不在, the current element is new and is added directly. The hash table was built to store the current longest value, {len:longestsub}
ifS[cur] Notinchlongestsub:longestsub+=S[cur] Self.createhashtable (longestsub)
#如果在, follow the procedure in parsing, removing elements before the repeating element, plus the current element to make up the longestsub.
#这里不用哈希操作是因为这里是减字符, there must be little, no need to save.
Else: Pos=Longestsub.index (S[cur]) longestsub= longestsub[pos+1:] +S[cur]
#字典中key为子串的长度, get the length, get the maximum, return it. Checkmaxlist=_dict.keys ()ifLen (checkmaxlist) = =0: return 0 Else: Longestint=Max (checkmaxlist)returnLongestint

This method runs at 144ms and is ranked in the middle of the python ranking. Later thought, there is no need to use the hash to save the current maximum, the subject as long as the maximum length can be returned, with a variable can be done. As for why the first to use the hash, is because of a look at the title tag, there is a hash ... The second method is a pure list implementation

2. List

1 classSolution:2# @param {string} s3 # @return {integer}4 def lengthoflongestsubstring (self, s):5Longestsub =""6Length =Len (s)7MaxLen =08_dict = {}9          forCurinchRange (length):Ten             ifS[cur] Notinchlongestsub: OneLongestsub + =S[cur] ALen_sub =Len (longestsub) -                 ifLen_sub >MaxLen: -MaxLen =len_sub the                 Else: - Pass -             Else: -pos =Longestsub.index (S[cur]) +Longestsub = Longestsub[pos +1:] +S[cur] -                  +         returnMaxLen

This is much more refreshing, execution time 104ms, in Python ranked in the top 10%, how can it be fast?

The current time complexity should be O (n^2), the first reaction is optimized to O (Nlogn), but this will have to have a binary search process. But the neutron string is not arranged in order, how to optimize it? It feels like the road is out of line.

Analysis of the next program, the sense of the cost should be found in the longestsub in the presence of duplicate characters, that is, if s[cur] in Longestsub, is traversed, so relatively slow. So I replaced this step with the search in the Hudson, the complexity of the search is not O (1)?

3, Hash

1 classSolution:2# @param {string} s3 # @return {integer}4 def lengthoflongestsubstring (self, s):5Longestsub =""6Length =Len (s)7MaxLen =08_dict = {}9          forCurinchRange (length):Ten#check Wheather The current character already existinchThe dict Onepos = _dict.Get(S[cur],'No') A#用字典方法查找, the complexity is O (1) -             ifpos = ="No": -Longestsub + =S[cur] the_dict[s[cur]] =Longestsub.index (S[cur]) -Len_dict =Len (_dict) -                 ifLen_dict >MaxLen: -MaxLen =len_dict +                 Else: - Pass +#inch, cut off the previous substring, and change the value by- A             Else:
#tempString为重复字符之前的子串, need to be deleted. Use tempstring to index, delete the corresponding characters in Dict attempstring = longestsub[: pos +1] -Longestsub = Longestsub[pos +1:] +S[cur] -#del previous characterinchDict. - forIinchtempstring: - _dict.pop (i) - forIinchlongestsub[:-1]: in_dict[i] = _dict[i]-pos-1
#新来的重复字符, because it has been deleted before the repetition, now this is not repeated, put in the last, with the subscript in Str as value -_dict[s[cur]] =Longestsub.index (S[cur]) to + returnMaxLen

The execution time of this method is 508ms ..... In the rankings have been on the back of the search. Why so slow, has solved the problem of the search. The analysis decision is slow here:

1  for inch tempstring: 2     _dict.pop (i)3 for in longestsub[:-1]:4     1

That is, to traverse the hash table, to delete the repeating substring, and to modify the subscript of the non-repeating substring. All right, look at this one, just ignore that one. Why do we need subscript?

Because the hash table is created using longestsub<string> to index, such as "ABC", in hash expressed as {' A ': 0, ' B ': 1, ' C ': 2}. So when there is repetition, such as ABCB, first get the repeat character B in the value of the hash is 1, this step completes the longestsub in the search, the complexity of O (1), solves the problem of method two. After the POS is 1, the "AB" is deleted, and the "CD" is reserved. Then just in hash pop (A, b), modify the value of hash C to 2-1-1=0, and add B, which is 1, that is, the hash becomes {' C ': 0, ' B ': 1}. In this way, hashes and longestsub are always corresponding and interdependent.

How to solve the problem of the hash change value, so that really play the effect of hashing it? See discuss find inspiration and see.

Vi. Summary

1. Know the method of defining sub-functions and using global variables in Leetcode

2, when their own quilt into the circle can not come out, discuss with others is very good, good method is discussed out, always be able to check the gaps, common progress.

3, there is an impulse to optimize the algorithm, to see exactly how to do so fast.

#3 longest Substring without repeating characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.