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.  
  11. public static int strFindAndCount(String gdzie, String co)
  12. {
  13. int licznikWystapien = 0;
  14. String[] tablicaSlow = new String[200];
  15. tablicaSlow = gdzie.split("\\s+");
  16.  
  17. for (int i = 0 ; i < tablicaSlow.length;i++)
  18. {
  19.  
  20. if(tablicaSlow[i].contains(co))
  21. {
  22. licznikWystapien++;
  23. }
  24.  
  25. }
  26. return licznikWystapien;
  27. }
  28.  
  29. public static int strFindAndCount2(String gdzie, String co)
  30. {
  31. if (co.length() <= 0 || gdzie.length() < co.length()) {
  32. return 0;
  33. }
  34.  
  35. int licznikWystapien = 0;
  36. int idx = 0;
  37.  
  38. while(idx >= 0 && (idx + co.length() <= gdzie.length())) {
  39. idx = gdzie.indexOf(co, idx);
  40. if (idx >= 0) {
  41. licznikWystapien++;
  42. idx++;
  43. }
  44. }
  45. return licznikWystapien;
  46. }
  47.  
  48. static int strFindAndCount3(String s, String p) {
  49. int cnt = 0;
  50. for (int i = 0; i < s.length() - p.length() + 1; ++i) {
  51. if (s.subSequence(i, i + p.length()).equals(p))
  52. ++cnt;
  53. }
  54. return cnt;
  55. }
  56.  
  57. private static void test(int testId, int methodId, String text, String pattern, int expected) {
  58. int result;
  59.  
  60. if (methodId == 1) {
  61. result = strFindAndCount(text, pattern);
  62. } else if (methodId == 2) {
  63. result = strFindAndCount2(text, pattern);
  64. } else {
  65. result = strFindAndCount3(text, pattern);
  66. }
  67.  
  68. if (result == expected) {
  69. System.out.printf("Test %d, method %d: OK%n", testId, methodId);
  70. } else {
  71. System.out.printf("Test %d, method %d: failed, expected: %d, result: %d%n", testId, methodId, expected, result);
  72. }
  73. }
  74.  
  75. private static void test(int testId, String text, String pattern, int expected) {
  76. test(testId, 1, text, pattern, expected);
  77. test(testId, 2, text, pattern, expected);
  78. test(testId, 3, text, pattern, expected);
  79. }
  80.  
  81. public static void main (String[] args) throws java.lang.Exception
  82. {
  83. test(1, "mama ma kota", "ma", 3);
  84. test(2, "mama ma kota", "", 0);
  85. test(3, "", "", 0);
  86. test(4, "mama", "mama", 1);
  87. test(5, "ma", "mama", 0);
  88. test(6, "mama", "m", 2);
  89. test(7, "mama, mamma mia", "ma", 4);
  90. test(8, "maaa, mia", "aa", 2);
  91. }
  92. }
Success #stdin #stdout 0.13s 34384KB
stdin
Standard input is empty
stdout
Test 1, method 1: failed, expected: 3, result: 2
Test 1, method 2: OK
Test 1, method 3: OK
Test 2, method 1: failed, expected: 0, result: 3
Test 2, method 2: OK
Test 2, method 3: failed, expected: 0, result: 13
Test 3, method 1: failed, expected: 0, result: 1
Test 3, method 2: OK
Test 3, method 3: failed, expected: 0, result: 1
Test 4, method 1: OK
Test 4, method 2: OK
Test 4, method 3: OK
Test 5, method 1: OK
Test 5, method 2: OK
Test 5, method 3: OK
Test 6, method 1: failed, expected: 2, result: 1
Test 6, method 2: OK
Test 6, method 3: OK
Test 7, method 1: failed, expected: 4, result: 2
Test 7, method 2: OK
Test 7, method 3: OK
Test 8, method 1: failed, expected: 2, result: 1
Test 8, method 2: OK
Test 8, method 3: OK