HashSet 记录是否包含 双指针 滑动窗口
import java.util.Set;
import java.util.HashSet;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main{
public static int lengthOfLongestSubstring(String s) {
/*哈希集合 ,记录每个字符是否出现过*/
Set<Character> occ = new HashSet<Character>();
int n = s.length();
/*右指针,初始值为-1,相当于在字符串的左侧 , 还没开始移动*/
/*rk 右边指针*/
int rightP = -1;
int ans = 0;
/*i 是leftP*/
for (int i = 0; i < n; i++) {
if (i!=0){
/*左指针左移动*/
occ.remove(s.charAt(i-1));
}
/*右指针 右移动*/
while (rightP+1<n &&(!occ.contains(s.charAt(rightP+1)))){
occ.add(s.charAt(rightP+1));
++rightP;
}
ans = Math.max(ans,rightP-i+1);
}
return ans;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
while((str = br.readLine()) != null)
System.out.println(lengthOfLongestSubstring(str));
}
}