java-最大不重复字符串长度

描述

给定一字符串,求其中最大不重复子串长度。
exp:
input:"",output:0
input:"aaa",output:1
input:"abcbabc",output:3

代码

public class Fun {
	public static int maxLenthNoRepeat(String str){
		if(str==null || str.isEmpty()){
			return 0;
		}
		Map<Character, Integer> map = new HashMap<Character, Integer>();
		int maxLength = 0;
		int current = 0;
		
		//循环字符串,取出每个字符
		for(int index=0; index < str.length(); index++){
			if(map.containsKey(str.charAt(index))){
	            		current = map.get(str.charAt(index)) + 1;
			}
			else{
	            		if((index-current+1)>maxLength){  
	                		maxLength=index-current+1;  
	            		}
			}
			map.put(str.charAt(index), index);
		}
		
		return maxLength;
	}
}



原文地址:https://www.cnblogs.com/enhe/p/12141724.html