No repeated character LeetCode.3- longest substring (Longest Substring Without Repeating Characters)

This is the first book of pleasure and delight 341 update, the first 365 Pian original

01 questions and look ready

LeetCode algorithm is introduced today in the Medium-level title of the second question Longest Substring Without Repeating Characters (overall title No. 3). Given a string to find the length of the longest no repeated character sub-string. E.g:

Input: "abcabcbb"
Output: 3
Description: The answer is "abc", the length of 3.

Input: "bbbbb"
Output: 1
Description: The answer is "b", a length of 1.

Input: "pwwkew"
Output: 3
Description: The answer is "wke", a length of 3. Please note that the answer must be a substring, "pwke" is a sub-sequence instead of a substring.

02 The first solution

Violent solution, using a two-pointer and HashSet.

Each time a character selected from the character onward traversal stored HashSetgo, means HashSetto determine whether repeated, if they are repeated characters, on the end of the cycle length of the index calculation start, then HashSetemptied, the cycle continues, and then start traversing from the second character, until all the characters have been processed.

The time complexity of this solution is the O(N^2)worst case time complexity O(N^3)because the method contains the HashSet; Space complexity is O(N).

public int lengthOfLongestSubstring(String s) {
    Set<Character> set = new HashSet<Character>();
    int n = s.length(), left = 0, right = 0;
    int result = 0;
    for (int i=0; i<n; i++) {
        left = i;
        right = i;
        while (right < n && !set.contains(s.charAt(right))) {
            set.add(s.charAt(right));
            right++;
        }
        result = Math.max(result, right-left);
        set.clear();
    }
    return result;
}


03 The second solution

For the first solution, we can also be HashSetreplaced by an array, use the array to determine whether the recurring characters, like any other ideas.

public int lengthOfLongestSubstring2(String s) {
    int[] set = new int[256];
    int n = s.length(), left = 0, right = 0;
    int result = 0;
    for (int i=0; i<n; i++) {
        left = i;
        right = i;
        while (right < n && ++set[s.charAt(right)] < 2) {
            right++;
        }
        result = Math.max(result, right-left);
        // 将数组元素值全部重置为0
        Arrays.fill(set, 0);
    }
    return result;
}


04 A third solution

Sliding window algorithm.

This solution and the above solution is somewhat similar to the first, using two variables, either side, like a window, like sliding, encounters duplicate values ​​will be removed from HashSet in a character to the left, until traversed all the characters .

The time complexity of this solution is the O(N)time complexity in the worst case O(N^2), since HashSetthe containsmethod; Space complexity is O(N).

public int lengthOfLongestSubstring3(String s) {
    Set<Character> set = new HashSet<Character>();
    int n = s.length(), left = 0, right = 0;
    int result = 0;
    while (left < n && right < n) {
        if (!set.contains(s.charAt(right))) {
            set.add(s.charAt(right));
            right++;
            result = Math.max(result, right-left);
        } else {
            set.remove(s.charAt(left));
            left++;
        }
    }
    return result;
}


05 The fourth solution

Index value is used to determine, at the same time HashSetinto the size of the array 256.

public int lengthOfLongestSubstring4(String s) {
    int[] set = new int[256];
    int n = s.length(), i = 0, j = 0;
    int result = 0;
    while (i < n && j < n) {
        i = Math.max(set[s.charAt(j)], i);
        result = Math.max(result, j-i+1);
        set[s.charAt(j)] = j+1;
        j++;
    }
    return result;
}


06 Summary

Thematic algorithm has been continuous days more than six months , the algorithm of feature articles 210 + articles, public Number dialog box reply [ data structures and algorithms ], [ algorithm ], [ data structures ] either a keyword to obtain a series of articles Collection .

That's all, if you have any good solution ideas, suggestions or other issues, you can exchange comments below, good-looking, message forwarding and support is the greatest reward for me!

Guess you like

Origin www.cnblogs.com/xiaochuan94/p/10963348.html