描述

给定一字符串,求其中最大不重复子串长度。
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;
	}
}



相关文章:

  • 2021-11-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
  • 2021-10-30
  • 2021-09-18
猜你喜欢
  • 2022-12-23
  • 2021-11-22
  • 2022-12-23
  • 2021-08-18
  • 2022-12-23
  • 2021-11-19
  • 2021-04-22
相关资源
相似解决方案