[LeetCode-interview algorithm classic-Java implementation] [003-Longest Substring Without Repeating Characters (Longest non-duplicate Substring)], longestsubstring

Source: Internet
Author: User

[LeetCode-interview algorithm classic-Java implementation] [003-Longest Substring Without Repeating Characters (Longest non-duplicate Substring)], longestsubstring
[003-Longest Substring Without Repeating Characters (maximum non-duplicate Substring )]Original question

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.

Theme

Returns the maximum number of non-duplicate substrings in a string.

Solutions

Use start to record the start position of the current processing string. When the current character has appeared since start, the start position of the substring is + 1, otherwise, the hash value in the map is updated to the current position.

Code Implementation
Import java. util. arrays; import java. util. hashMap; import java. util. map;/*** Author: Wang junchao * Date: 2015-06-17 * Time: 20:46 * Declaration: All Rights Reserved !!! */Public class Solution {/*** 003-Longest Substring Without Repeating Characters (maximum non-duplicate Substring) ** @ param s input String * @ return maximum length of a non-duplicate substring * // can process all UTF-8 characters public int lengthOfLongestSubstring (String s) {// The string input is invalid if (s = null) {return 0;} // the start position of the current processing int start = 0; // The maximum length of the non-duplicate substring recorded int result = 0; // access tag, record the last accessed Character and position Map <Character, integer> map = new HashMap <> (s. length (); for (int I = 0; I <s. length (); I ++) {char ch = s. charAt (I); // if the character already exists (starting from the starting position), remark start if (map. containsKey (ch) & map. get (ch)> = start) {start = map. get (ch) + 1 ;}// if no such occurrence occurs, calculate the maximum length of the non-duplicate substring else {result = Math. max (result, I-start + 1);} // update the access record map. put (ch, I);} return result;} // only ASCII characters are considered. [solution 2] public int lengthOfLongestSubstring2 (String s) {// The string input is invalid if (s = null) {return 0;} // indicates whether the mark character exists, and the location of the last accessed element is recorded as int [] appear = new int [256]; // It is initialized to-1 Arrays. fill (appear,-1); // start position of the current processing int start = 0; // Save the result int result = 0; for (int I = 0; I <s. length (); I ++) {// if the character already exists (in the marked open position), re-mark start if (appear [s. charAt (I)]> = start) {start = I + 1;} // if there is no such occurrence, calculate the maximum length of the non-duplicate substring else {result = Math. max (result, I-start + 1);} // mark that the I character has been accessed (the latest is the I position) appear [s. charAt (I)] = I;} return result ;}}
Evaluation Result

  Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.

Note Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/46921991]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.