fork download
  1. //The solutions should be written, for compiler compatibility : j2se jdk 8u51
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. public class Main
  7. {
  8. static final int MAX = 10000000;
  9.  
  10. static boolean compare(char arr1[], char arr2[])
  11. {
  12. for (int i = 0; i < MAX; i++)
  13. if (arr1[i] != arr2[i])
  14. return false;
  15. return true;
  16. }
  17.  
  18. static void search(String pat, String txt)
  19. {
  20. int M = pat.length();
  21. int N = txt.length();
  22. // System.out.println("A");
  23. char[] countP = new char[MAX];
  24. char[] countTW = new char[MAX];
  25. // System.out.println("A");
  26. for (int i = 0; i < M; i++)
  27. {
  28. countP[(int)pat.charAt(i)]++;
  29. countTW[(int)txt.charAt(i)]++;
  30. }
  31. // System.out.println("B");
  32. for (int i = M; i < N; i++)
  33. {
  34.  
  35. if (compare(countP, countTW))
  36. System.out.println(String.valueOf((i - M)));
  37.  
  38. (countTW[txt.charAt(i)])++;
  39.  
  40. countTW[txt.charAt(i-M)]--;
  41. }
  42. // System.out.println("C");
  43. if (compare(countP, countTW))
  44. System.out.println(String.valueOf((N - M)));
  45. }
  46. public static void main (String[] args) throws java.lang.Exception
  47. {
  48. //use the following code to fetch input from console
  49. String line;
  50.  
  51. Scanner in = new Scanner(System.in);
  52. String a = in.next();
  53. String b = in.next();
  54. search(b,a);
  55. }
  56. }
Success #stdin #stdout 0.13s 74952KB
stdin
AAABABAA
AABA
stdout
0
1
4