[Leetcode] Longest substring without repeating characters

Source: Internet
Author: User

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 is "B", with the length of 1.

Solution 1.

Let's look at an example:

S = "abbdeca ".

T1 = "abbdeca", T1 [1] = T1 [2].

T2 = "bbdeca", T2 [0] = t2 [1].

T3 = "bdeca", always scan until the end.

T4 = "Deca", T5, T6, and T7 are all the same as above.

We have scanned s [2] when processing T1, and then scanned s [2] to s [6] when processing T3. the two substrings have scanned the entire parent string.

In other words, there are only two locations for the substring to stop scanning: 1.s[ 2]; 2.s[ 6] (end ).

For another example, S = "aaab", the locations where the substring stops scanning are: s [1], s [2], s [3] (end ).

 

Therefore, we can consider scanning only the parent string to retrieve the longest non-duplicate substring from the parent string.

For s [I]:

1. s [I] does not appear in the current substring, so the length of the substring is increased by 1;

2. s [I] appears in the current substring and the subscript of the position is J. Therefore, the starting position of the new substring must be greater than J. To make the new substring as long as possible, therefore, select J + 1 as the starting position.

** Note that the character range is 256. You cannot define only 27 or 30 characters, because in the test case, there are not only letters, but also other symbols!

// Longestsubstring. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <string >#include <iostream> using namespace STD; Class solution {public: int lengthoflongestsubstring (string s) {int posarray [256]; int max = 0; memset (posarray,-1, sizeof (posarray); int Pa =-1; for (INT I = 0; I <S. size (); I ++) {If (posarray [s [I]> Pa) {Pa = posarray [s [I];} if (I-Pa> MAX) max = I-PA; posarray [s [I] = I;} return Max ;}; int _ tmain (INT argc, _ tchar * argv []) {string STR = "BB"; solution SS; int max = ss. lengthoflongestsubstring (STR); cout <max <Endl; System ("pause"); Return 0 ;}

  

[Leetcode] 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.