Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int count(String str, String pattern) {
- //!IMPORTANT!!!
- if (str.length() < pattern.length())
- return 0;
- int count = countHelper(str, pattern, 0, 0);
- return count;
- }
- private static int countHelper(String s, String p, int i, int j) {
- int count = 0;
- if (i <= s.length() - 1) {
- char charS = s.charAt(i);
- char charP = s.charAt(j);
- //if match
- if (charS == charP) {
- System.out.println(charS + " " + charP + "\n");
- //if final match
- if (j == p.length() - 1) {
- count = count + 1;
- System.out.println(count);
- count += countHelper(s, p, i + 1, j);
- }
- else {
- count += countHelper(s, p, i + 1, j);
- count += countHelper(s, p, i + 1, j + 1);
- }
- }
- else {
- count += countHelper( s, p, i + 1, j);
- }
- }
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement