最长无重复子串的长度
package com.jareny.jave.algorithm.string;
import java.util.HashMap;
import java.util.Map;
/**
* 最长无重复子串的长度
*/
public class LengthOfLongestSubstring {
public static void main(String[] args) {
String str = "abcabcbb";
int max = longest(str);
System.out.println(max);
}
public static int longest(String str) {
int fast = 0;
int slow = 0;
int max = 0;
int length = str.length();
Map<Character, Integer> map = new HashMap<>();
while (fast < length) {
char charAt = str.charAt(fast);
if (map.containsKey(charAt)) {
slow = map.get(charAt)+1;
}
map.put(charAt, fast);
max = Math.max(fast - slow +1, max);
fast++;
}
return max;
}
}