Longest Substring Without Repeating Characters the maximum number of non-Repeating Characters @ L

Source: Internet
Author: User

Method 1 (Simple)
We can consider all substrings one by one and check for each substring whether it contains all unique characters or not. there will be n * (n + 1)/2 substrings. whether a substirng contains all unique characters or not can be checked in linear time by scanning it from left to right and keeping a map of visited characters. time complexity of this solution wocould be O (n ^ 3 ).

Method 2 (Linear Time)
Let us talk about the linear time solution now. this solution uses extra space to store the last indexes of already visited characters. the idea is to scan the string from left to right, keep track of the maximum length Non-Repeating Character Substring (NRCS) seen so far. let the maximum length be max_len. when we traverse the string, we also keep track of length of the current NRCS using cur_len variable. for every new character, we look for it in already processed part of the string (A temp array called visited [] is used for this purpose ). if it is not present, then we increase the cur_len by 1. if present, then there are two cases:

A)The previous instance of character is not part of current NRCS (The NRCS which is under process). In this case, we need to simply increase cur_len by 1.
B)If the previous instance is part of the current NRCS, then our current NRCS changes. it becomes the substring staring from the next character of previous instance to currently scanned character. we also need to compare cur_len and max_len, before changing current NRCS (or changing cur_len ).



Http://www.geeksforgeeks.org/length-of-the-longest-substring-without-repeating-characters/


Package Level3; import java. util. arrays;/*** 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 letters for "abcabcbb" is "abc", which the length is 3. for "bbbbb" the longest substring is "B", with the length of 1. **/public class S3 {public static void main (String [] args) {String s = "abcbad"; System. out. println (lengthOfLongestSubstring (s);} // http://www.geeksforgeeks.org/length-of-the-longest-substring-without-repeating-characters/// O (n), O (1) public static int lengthOfLongestSubstring (String s) {if (s = null | s. length () = 0) {return 0;} // visited [char's ASCII] = char's index int [] visited = new int [256]; Arrays. fill (visited,-1); int curLen = 1; int maxLen = 1; int prevIndex = 0; visited [s. charAt (0)] = 0; for (int I = 1; I
 
  

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.