Leetcode double pointer longest string without repeated characters java

Title description
Given a string, please find out the length of the longest substring that does not contain repeated characters.
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: Because the longest substring without repeated characters is "abc", its length is 3.

Method 1:
Create a new hashset, initialize two pointers i, j, i represent the starting position of the sliding window, j represents the ending position of the sliding window, and num represents the length of the returned string.
If the set does not contain the j-th letter, then the j-th letter is added to the set, j+1, num is the larger value of num and ji.
If the set contains the j-th letter, the window will be sliding The starting position of +1, and then continue to judge [Note: j did not do the plus one operation, so the next cycle will continue the same judgment as the last cycle until there are no repeated elements in the window] For
example:
abbcd for the
first time Add a to the set, i = 0, j =1,
add b to the set for the second time, i = 0 j = 2 the
third time s.charAt(2) = b set contains b, so i +1 = 1
fourth The second s.charAt(2) = b set contains b, so i + 1 = 2 The
fifth time s.charAt(2) = bi = 2 j+1 = 3
…It is
equivalent to a sliding window that first becomes larger and then becomes smaller The process of getting bigger

Code:

class Solution {
    
    
    public int lengthOfLongestSubstring(String s) {
    
    
        int num = 0;
        Set<Character> set = new HashSet<>();//创建滑动窗口
        int j=0,i = 0;
        while(i<s.length()&&j<s.length()){
    
    
            if(!set.contains(s.charAt(j))){
    
    
                set.add(s.charAt(j));
                j++;
                num = Math.max(num,j-i);
            }else{
    
    
                set.remove(s.charAt(i));
                i++;
            }
        }
        return num;
    }
}

Method 2:
If there is a duplicate element, the initial position pointer points to the next position
that has appeared. Because the subscript of the initial position needs to be updated instead of adding one as above, HashMap, map.get(s.charAt) is needed. (i)) For
example:
abbcd
first time i=0 add (a,0) to map
second time i=1 add (b,1)
third time i=2 map contains b, then left = max (0,1+1)=2, then add (b,2) to the map [Note: the key of the map will not be repeated, so it will overwrite (b,1)], the elements in the current map are (a,0) And (b, 2) the
fourth time i = 3 add (c, 3) to the map the
fifth time i = 4 add (d, 4) to the map

class Solution {
    
    
    public int lengthOfLongestSubstring(String s) {
    
    
        int num = 0;
        Map<Character,Integer> map = new HashMap<>();    
        int left = 0;
        for(int i = 0;i<s.length();i++){
    
    
            if(map.containsKey(s.charAt(i))){
    
    
                left = Math.max(left,map.get(s.charAt(i))+1);
            }
            map.put(s.charAt(i),i);
            num = Math.max(num,i-left+1);
        }
         
        return num;

    }
}

Guess you like

Origin blog.csdn.net/stonney/article/details/111029976