Advertisement
bokoness

שאלה 3 ממן 14

Jun 9th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1.  
  2.     public static int count(String str, String pattern) {
  3.         //!IMPORTANT!!!
  4.         if (str.length() < pattern.length())
  5.             return 0;
  6.         int count = countHelper(str, pattern, 0, 0);
  7.         return count;
  8.     }
  9.  
  10.     private static int countHelper(String s, String p, int i, int j) {
  11.         int count = 0;
  12.  
  13.         if (i <= s.length() - 1) {
  14.  
  15.             char charS = s.charAt(i);
  16.             char charP = s.charAt(j);
  17.  
  18.             //if match
  19.             if (charS == charP) {
  20.                 System.out.println(charS + " " + charP + "\n");
  21.                 //if final match
  22.                 if (j == p.length() - 1) {
  23.                     count = count + 1;
  24.                     System.out.println(count);
  25.                     count += countHelper(s, p, i + 1, j);
  26.                 }
  27.  
  28.                 else {
  29.                     count += countHelper(s, p, i + 1, j);
  30.                     count += countHelper(s, p, i + 1, j + 1);
  31.                 }
  32.             }
  33.             else {
  34.                 count += countHelper( s, p, i + 1, j);
  35.             }
  36.         }
  37.         return count;
  38.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement