Leetcode third question _longest Substring without repeating characters

Source: Internet
Author: User

Longest Substring without repeating characters total

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "ABCABCBB" are "abc", which the length is 3. For "bbbbb" the longest substring are "B", with the length of 1.

This is the third question of the Leetcode algorithm, the topic is probably meant to give you a string, you want to find the longest, no duplicate substring.

My initial idea of this topic is to use the idea of dynamic programming, design a window, the window contains the string is no repetition, and then the window has been moved until the last character, in this process found the longest no repetition substring.

The implementation is using the I record the left end of the window, J record the right end of the window, if the J-node character and the preceding characters do not repeat, then J right, expand the window. If the character of the J-node repeats with the preceding character, I moves to the next node of the same character node as the J-node and the left-hand side of the window.

In this process, Max is used to record the length of the substring, and each time J changes, the size of the j-i and Max is compared, and the large value is assigned to Max.

But in the realization of this idea, encountered a problem: I am still too foolish and naïve, did not find a good way to determine whether the J-node character and the preceding character is duplicated. Think of its solution, traverse it, too slow, but to achieve complex, so it took a long time did not solve, looked at the knot idea.

Then, spicy, really day dog, this can also use hashmap!!! This is really shameless, this is not open hanging, unexpectedly is to use Haspmap character as key, the subscript of the character as value, and then, when the key value repeats, put will return the original key associated value, do not repeat the return null, it is easy to judge, I feel the world's malice.

Well, it is really me more strange, and then check again, just know the dynamic planning +hashmap is to solve the most sub-string, and the very excellent general solution of such problems. I want to be quiet.

The code is as follows

public static int lengthOfLongestSubstring(String s) {    int i = 0;    int j = 0;    int max = 0;    HashMap<Character, Integer> map  = new HashMap<Character, Integer>();//      当右端点到达字符串最后是结束循环    while (j<s.length()) {        Object k = map.put(s.charAt(j), j);//          如果有重复字符串,左端点移动到重复字符的下一个节点位置        if (k != null) {//              处理特殊情况,若后面字符跟已经跳过的前面字符重复,拒绝回溯            if (i<(Integer)k+1) {                i = (Integer)k+1;            }            map.put(s.charAt(i), i);        }        j++;//          如果新的子串比前面的最长子串还要长,交换长度        if (j - i > max) {            max = j - i;        }               }    return max;}

What to do! Still have to submit several times, see case will change program, oneself think of is not so comprehensive! How to do, how can I ac?!

Leetcode third question _longest Substring without repeating characters

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.