Find the maximum string without repeating the string

import java.util.*;
public class Client {
    public static void main(String[] args) {
        String str = "abcdbe";
        int i = maxSubLen(str);
        System.out.println(i);
    }

    /**
     * 求不重复字符的长度
     */
    static int maxSubLen(String str) {
        int maxLen = 0;
        int left = 0;
        int right = 0;
        char[] chars = str.toCharArray();
        int n = str.length();
        Object mapVal = new Object();
        Map<Character, Object> map = new HashMap<>();

        while (right < n) {
            Object val = map.get(chars[right]);
            if (val == null) {// no repeated characters 
                map.put (chars [right], mapVal); 
                right ++; 
                maxLen = Math.max (right-left, maxLen); 
            } else {// has repeated characters 
                while ( chars [left]! = chars [right]) { 
                    left ++; 
                } 
                left ++; 
                right ++; 
            } 
        } 
        return maxLen; 
    } 

}

 

Guess you like

Origin www.cnblogs.com/dongma/p/12730170.html