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;
}