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. char[] Sequence = new char[] { 'a', 'b', 'a', 'a', 'a',
  13. 'b', 'a', 'c', 'c', 'c', 'a', 'a', 'b', 'b', 'a', 'c', 'c', 'a', 'a',
  14. 'b', 'a', 'b', 'a', 'c', 'a', 'a', 'b', 'a', 'b', 'a', 'a', 'c' };
  15. char[] Pattern1 = new char[] { 'a', 'a', 'b' };
  16. Integer[] results = match(Sequence, Pattern1);
  17. for(int i:results) {
  18. System.out.println(i);
  19. }
  20. }
  21.  
  22. public static Integer[] match(char[] T, char[] P) {
  23. List<Integer> results = new ArrayList<>();
  24. boolean match;
  25. for (int s = 0; s <= (T.length - P.length); s++) {
  26. match = true;
  27. for (int j = 0; j <= P.length - 1; j++) {
  28. if (T[s + j] != P[j]) {
  29. match = false;
  30. break;
  31. }
  32. }
  33. if (match == true) {
  34. results.add(s);
  35. }
  36. }
  37. return results.toArray(new Integer[results.size()]);
  38. }
  39. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
3
10
17
24