[Leetcode] Longest substring without repeating characters (linkedhashset)

Source: Internet
Author: User

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.


Topics involving longest substring generally have obvious DP features. A one-dimensional DP approach is to classify all non-repeated substrings by the ending array subscript, so that maxendswith [I] indicates that the end of the string uses I as the array subscript, and take the longest length.

For a given string that does not contain repeated characters, and a new character that tries to be added at the end:

1) if the new character is not the same as the character in the given string, the maximum length is 1, that is, maxendswith [I] = maxends [I-1] + 1;

2) if a new duplicate is added, you must delete the previous duplicate characters and do not count the characters before the duplicate characters.

This idea actually looks like a data stream algorithm. The core is to maintain a sliding window to ensure that all elements in the window are not repeated. It is best to use set when deduplication is involved.HoweverThe difficulty here is that after the duplicate characters are found, not only do you delete the duplicate characters, but also all the characters before them.

For Deletion operations, you can use a very "awkward" implementation method, that is, you can use map, key storage character, and value storage index instead of set, then, all the indexes included in the map are stored in a queue (or deque. In this way, you must modify both map and queue for both insertion and deletion.

In fact, there can be a more concise and elegant implementation method for this DP, that is, usingLinkedhashsetThe data result. Unlike hashset,Linkedhashset concatenates each element into a linked list based on the element insertion sequence. Therefore, the insertion sequence is strictly followed during traversal.As a result, the advantage of using the repeated hashset is obvious. Because we maintain a sliding window, the characters before the repeated characters must be inserted in the window, the insertion sequence and traversal sequence are both at the beginning of the repeated characters. Therefore, once repeated characters are encountered, they can be deleted from the beginning of the sliding window, always in the traversal order, and deleted together.

public int lengthOfLongestSubstring(String s) {if (s.length() == 0)return 0;int ret = 1;Set<Character> set = new LinkedHashSet<Character>();int[] maxEndsWith = new int[s.length()];maxEndsWith[0] = 1;set.add(s.charAt(0));for (int i = 1; i < s.length(); ++i) {char c = s.charAt(i);if (!set.contains(c)) {set.add(c);maxEndsWith[i] = maxEndsWith[i - 1] + 1;} else {Iterator<Character> it = set.iterator();while (it.hasNext()) {char front = it.next();it.remove();if (front == c) {break;}}set.add(c);maxEndsWith[i] = set.size();}ret = Math.max(maxEndsWith[i], ret);}return ret;}

Delete repeated characters in the traversal order and delete them together. Note that the above Code has a two-layer loop, but the time complexity is O (n), because each character is only added to the window once, it will only be deleted once from the window. In addition, the data is only scanned once in total, which is characteristic of a typical data stream algorithm.



[Leetcode] Longest substring without repeating characters (linkedhashset)

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.