给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Map<Character, Integer> map = new HashMap<>();
String s = in.next();
int res = 0;
for (int start = 0, end = 0; end < s.length(); end ++ ) {
char c = s.charAt(end);
if (map.containsKey(c)) {
start = Math.max(start, map.get(c));
}
res = Math.max(res, end - start + 1);
map.put(s.charAt(end), end + 1);
}
System.out.println(res);
}
} import java.util.*;
public class Main{
public static void main(String[] args){
Map<Character,Integer> map = new HashMap<>();
Scanner sc = new Scanner(System.in);
String s =sc.next();
int left =0; int max=0;
for(int i = 0; i< s.length();i++){
if( map.containsKey( s.charAt(i) ) ){
left = Math.max(left, map.get( s.charAt(i) )+1);
}
map.put(s.charAt(i),i);
max = Math.max(max, i - left +1);
}
System.out.println(max);
}
} public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String str = scanner.next();
int answer = getAnswer(str.toCharArray());
System.out.println(answer);
}
}
public static int getAnswer(char[] str) {
Map<Character, Integer> lastIndexMap = new HashMap<>(26);
int answer = 0;
int tempAnswer = 0;
for (int i=0; i<str.length; i++) {
// 1、计算值
Integer lastIndex = lastIndexMap.get(str[i]);
if (lastIndex == null) {
// 如果从未出现过
tempAnswer += 1;
} else {
// 以i-1为Right时候,Left值
int tempAnswerLeft = i - tempAnswer;
// 下面判断char[i]是否有在[left, right]中出现过
if (lastIndex < tempAnswerLeft) {
tempAnswer++;
} else {
tempAnswer = i - lastIndex;
}
}
// 2、取最大
answer = Math.max(answer, tempAnswer);
// 3、更新lastIndexMap
lastIndexMap.put(str[i], i);
}
return answer;
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
String inPut = sc.next();
String ansStr = "";
int ans = 0;
String temp = "";
int temp1 = 0;
for(int i = 0;i < inPut.length();i ++)
{
if(temp.indexOf(inPut.charAt(i)) == -1)
{
temp += inPut.charAt(i);
temp1++;
}else{
temp = "";
temp1 = 0;
}
if(ans < temp1)
{
ans = temp1;
}
}
System.out.println(ans);
}
}
}