title Chinese : Oldest string without repeating character
Topic difficulty : Medium
topic content :
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer "abc"
is, which the length is 3.
Given "bbbbb"
, the answer "b"
is, with the length of 1.
Given "pwwkew"
, the answer "wke"
is, with the length of 3. Note that the answer must was a substring, is "pwke"
a subsequence and not a substring.
translation :
Given a string, the length of the oldest string is computed, and the character cannot be repeated within the substring.
Example:
Given "ABCABCBB", the answer is "ABC" with a length of 3.
Given "bbbbb", the answer is "B" and the length is 1.
Given "Pwwkew", the answer is "Wke" and the length is 3. Note that the answer must be a substring (continuous), and "Pwke" is a subsequence, not a substring.
idea : should be a substring (continuous), so the double loop can traverse each substring, and then use a function to determine if there are duplicates within the substring, no update the latest length
My Code :
1 Public intlengthoflongestsubstring (String s) {2 intn = s.length ();//size (), length () This kind of function is not like the variable length, which is also to be computed, when called more than once, assigned to a variable to reduce the complexity of3 intAns = 0;4 for(inti = 0; I < n; i++) {5 for(intj = i + 1; J <= N; J + +) {6 if(IsUnique (S, I, J)) {7Ans = math.max (ans, j-i);//if unique, update ans8 }9 }Ten } One returnans; A } - - Public Static BooleanIsUnique (String S,intStartintend) { theSet<character> Hset =NewHashset<character>(); - for(inti = start; I < end; i++) { - Hset.add (S.charat (i)); - } + returnHset.size () = = (End-start)?true:false;//if the size of the set is not changed, the description is unique. -}
982/983 Test cases passed. Status:time Limit exceeded
Timed out ... Obviously the complexity of this algorithm is O (N3), really big point ...
issue during encoding :
1, the boundary problem of the program troubled for a long time, the beginning IsUnique method I was set from I to J all judgment, J to n-1 end, the results found that such a child needs to add a lot of boundary conditions, such as the input is "C" on one, then J from subscript 1 directly on the cross will not enter the loop body . or all the same character "CCCCCCCCCC", IsUnique will only always be false,ans will not be updated . Peeking at the reference to the answer, found the first one and I almost the same, carefully see that it is the only range from I to J of the previous character, J range from i+1 to n, which will always enter the loop body, and ans at least updated.
Answer one :
Code slightly
Almost the same as mine is the only way to judge it is this:
public boolean allunique (String s, Span style= "COLOR: #0000ff" >int start, int End) {Set <Character> set = new hashset<character> (); for (int i = start; i < end; i++< Span style= "color: #000000") {Character ch = S.charat (i); if (Set.contains (CH)) return false ; Set.add (CH); return true ; }
It might be a little less complicated than my algorithm, because it can be returned directly when it is found, and my one is all added before I can judge the repetition. Remember that both set and map have contains functions .
answer two :
1 Public Static intlengthOfLongestSubstring2 (String s) {2 intn = s.length (), ans = 0;3Map<character, integer> map =NewHashmap<character, integer>();4 for(intj = 0, i = 0; J < N; J + +) {5 if(Map.containskey (S.charat (j))) {6i =Math.max (Map.get (S.charat (j)), I);7 }8Ans = math.max (ans, j-i + 1);9Map.put (S.charat (j), J + 1);Ten } One returnans; A}
983/983 Test Cases passed. Runtime: ms beats 25.38% algorithm complexity: O (N)
idea : Using the map's key-value structure to store values and corresponding cis (subscript + 1), and then every time the pointer (j) down to determine whether the character has been included, if it is the value of I update, so each ans is the largest i to J Length of the (including J).
So I point at the beginning of the substring that does not contain the repetition, and the J point is the end. Each update of ANS ensures that ans is the maximum length.
such as Abcdaa
When j = 4 o'clock, it is found that the map has a at this point J, so update the value of I, indicating that the previous a in the 1th, so ans is equal to the position of the new A (j) minus the position of the old A (cis-i-1), so the equivalent of j-i +1 = 4.
When j = 5 o'clock, it is found that the map has a at this point J, so update the value of I, indicating that the previous a in the 5th, so ans_temp = 5-5+1 = 1, take max, ans eventually equals 4.
Why not just save the label?
If the storage is subscript, then the last update of ANS can only be j-i (new position minus the old position), if the entire string is not duplicated, then the final answer is length-1-0, because only when there is an update to the subscript can be directly subtracted.
answer three :
1 Public intlengthoflongestsubstring (String s) {2 intn = s.length (), ans = 0;3 int[] index =New int[256];//Current index of character4 //try to extend the range [I, J]5 for(intj = 0, i = 0; J < N; J + +) {6i =Math.max (Index[s.charat (j)], i);7Ans = math.max (ans, j-i + 1);8Index[s.charat (j)] = j + 1;9 }Ten returnans; One}
983/983 Test cases passed. Runtime: 83.43% ms beats algorithm complexity: O (N)
Actually the thought and the method two are similar, a bit trickery nature ... Because the characters can be expressed in Ascall code , and the Ascall code altogether 256, each one represents a character, (because the latter 128 keyboard input can not, with 128 also line) so directly with an int array when the hash table is used , Reduces the complexity of addressing time for hash tables.
After the key to meet the hash table is a single character, you can think of using int[256] to replace the map.get (*);
Leetcode [3] (Java): Longest Substring without repeating characters tags: Linked List