尺取法,字符串
import java.io.IOException; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); int charr[] = new int[26]; int min = 999898989; if(s.length() <= 26){ System.out.println(26); }else{ int sum = 0,l = 0,r = 0; while(l < s.length()- 25 && r < s.length()){ if(sum < 26) { if (charr[s.charAt(r) - 'a'] == 0) { sum++; } charr[s.charAt(r) - 'a']++; if (sum < 26) { r++; } else { min = Math.min(r - l + 1, min); } } if(sum >= 26){ charr[s.charAt(l) - 'a']--; if(charr[s.charAt(l) - 'a'] == 0){ sum--; } if(sum < 26){ min = Math.min(r - l + 1,min); r++; } l++; } } System.out.println(min); } } }