fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner sc=new Scanner(System.in);
  13. String s=sc.next();
  14. int N=sc.nextInt();
  15. int si=0,ei=0,maxlen=0;
  16. HashSet<Character> arr=new HashSet<>();
  17. while(ei<N){
  18. char ch=s.charAt(ei);
  19. if(arr.contains(ch)){
  20. while(s.charAt(si)!=ch){
  21. arr.remove(s.charAt(si));
  22. si++;
  23. }
  24. arr.remove(s.charAt(si));
  25. si++;
  26. }
  27.  
  28. arr.add(s.charAt(ei));
  29. maxlen=Math.max(maxlen,arr.size());
  30. ei++;
  31. }
  32. System.out.println(maxlen);
  33. }
  34. }
Success #stdin #stdout 0.11s 56756KB
stdin
abcdef
6
stdout
6