Longest Substring without repeating characters
- Total accepted:167158
- Total submissions:735821
- Difficulty:medium
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.
Ideas for solving problems from: http://www.geeksforgeeks.org/length-of-the-longest-substring-without-repeating-characters/
1 Public classNum3 {2 /*3 * Method One: Brute force search, Complexity O (n^3)4 */5 Public intlengthoflongestsubstring (String s) {6 if(s = =NULL|| S.length () = = 0){7 return0 ;8 }9 String Sub;Ten for(intSublen = S.length (); Sublen > 0; sublen--){ One for(intStartIndex = 0; StartIndex <= (s.length ()-sublen); startindex++){ A //lists all substrings, and then determines if the substring satisfies a duplicate - if(StartIndex! = (S.length ()-Sublen)) { -Sub = s.substring (StartIndex, startindex+Sublen); the}Else{ -Sub =s.substring (startIndex); - } - if(!Isrepeat (sub)) { + returnSublen; - } + } A } at - return1 ; - } - - Private BooleanIsrepeat (String s) { - for(inti = 1; I < s.length (); i++){ in if(S.substring (i). Contains (S.substring (i-1, i))) { - return true ; to } + } - return false ; the } * $ /*Panax Notoginseng * Method Two: Using hash method plus dynamic programming solution - */ the Public intlengthOfLongestSubstring2 (String s) { + if(s = =NULL|| S.length () = = 0){ A return0 ; the } + intCur_len = 1;//lenght of current substring - intMax_len = 1 ; $ intPrev_index;//Previous Index $ int[] visited =New int[256] ; - Char[] arr =S.tochararray (); - /*Initialize the visited array as-1, 1 is used to the indicate that character have not been visited yet.*/ - for(inti = 0; I < 256; i++){WuyiVisited[i] = 1 ; the } - /*Mark first character as visited by storing the index Wu of first character in visited array.*/ -Visited[arr[0]] = 0 ; About $ /*Start from the second character. First character is - already processed (Cur_len and Max_len are initialized - as 1, and Visited[arr[0]] is set*/ - for(inti = 1; i < arr.length; i++){ APrev_index =Visited[arr[i]]; + the /*If The current character isn't present in the - already processed substring or it is not part of $ the current NRCS and then do cur_len++*/ the if(Prev_index = =-1 | | I-cur_len >Prev_index) { thecur_len++ ; the}Else{ the /*Also, when we are changing the NRCS, we - should also check whether length of the in previous NRCS was greater than Max_len or the Not .*/ the if(Cur_len >Max_len) { AboutMax_len =Cur_len; the } the //Update the index of current character theCur_len = i-Prev_index; + } - theVisited[arr[i]] =i;Bayi } the the //Compare The length of last NRCS with Max_len and - //update Max_len if needed - if(Cur_len >Max_len) { theMax_len =Cur_len; the } the the returnMax_len; - the } the the}
No.3 longest Substring without repeating characters