Leetcode#3 longest Substring without repeating characters

Source: Internet
Author: User

Problem Definition:

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without

Repeating letters for ' ABCABCBB ' is ' abc ', which the length is 3. For "bbbbb" the longest substring are "B", with the length of 1.

Solution:

In fact, the qualifying substring is a string sandwiched between two identical letters and without the same character (in extreme cases, the entire string). such as a*****abcd***a.

You can use both cursors to scan the string, using the spacing of the two cursors to the length of the operator string. The cursor on the right continues to move to the right until the end of the string, and the left cursor is updated only when the new substring is entered.

These actions are also performed during the sweep: record the scanned characters and their coordinates, and update the length of the longest non-repeating substring.

1) Start scanning j==0, i==0

2) I begin to move to the right, J is not yet moved. As I moves, the length of the non-repeating substring also increases. The current substring length is i-j+1. The length of the maximum number of non-repeating substrings should be updated in real time.

3) The character D has been swept in front, and when a character d is swept again, the Zoo should be updated to start a new string. Put the Zoo in the last position of the previous d, where the new string starts.

Also, the positional record of the character D is updated to the right position.

4) The right cursor continues to move forward, reaching a character a, and discovering that it has previously appeared, but the previous occurrence of the position has again left the coordinates of the cursor, belonging to the old string, does not affect the new string, so does not update Zoo J.

5) The right cursor is still moving forward until it encounters a character F, it has already appeared, and this position is on the right side of J, which belongs to the new string, so update J to f the last occurrence of the position behind.

The code is simple to write.

    # @param {string} s    # @return {integer}    def lengthoflongestsubstring (s):        dic,length,j= {},0,0for in          Enumerate (s):            if in DiC:                J=max (j,dic[c]+1)            dic[c]=i            length=max (length, i-j+1)        return length

Leetcode#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.