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

 

The O (n) solution is obtained by using map to record key information:

Class solution {public: int lengthoflongestsubstring (string s) {If (S. size () <2) return S. size (); int lengthofcurrent = 1, maxlength = 1; // lengthofcurrent indicates the number of unique characters before the current character. Map <char, int> m; // subscript of characters and characters in S stored in M. M [s [0] = 0; For (INT I = 1; I <S. size (); I ++) {If (M. count (s [I]) = 0 | M [s [I] <I-lengthofcurrent) // The current character is not repeated or repeated, but before the previous character is repeated, the current maximum length of lengthofcurrent ++ is not affected; else lengthofcurrent = I-m [s [I]; m [s [I] = I; If (lengthofcurrent> maxlength) maxlength = lengthofcurrent;} return maxlength ;}};

 

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