Leetcode Topic Record-3 substring with no repeating character in string __ the longest substring in the string without repeating characters

Source: Internet
Author: User
longest Substring without repeating Characters

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

"Analysis-not original" reference: Https://oj.leetcode.com/discuss/10747/my-accepted-solution-in-java

/* The longest substring with no repeating element. Find the current string inside the result string, if not, add to the result string, continue; If so, find its place in the result string, and add the current character to the result string; The system function to find the character subscript with the string. INDEXOF () * *

public static int lengthOfLongestSubString2 (String s) {

if (s.length () = = 0)

return 0;

if (s.length () = = 1)

return 1;

int length = 0;

String part = "" + s.charat (0);

for (int i = 1; i < s.length (); i++) {

if (Part.indexof (S.charat (i)) = = 1) {

Part = part + S.charat (i);

} else {

int index = Part.indexof (S.charat (i));

if (Part.length () > Length) {

Length = Part.length ();

}

Part = part.substring (index + 1);

i--;

}

}

return part.length () > length? Part.length (): length;

}

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.