forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecursionCount.java
34 lines (30 loc) · 913 Bytes
/
RecursionCount.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Scanner;
public class RecursionCount {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter first string:");
String str1 = scanner.nextLine();
System.out.println("Enter second string:");
String str2 = scanner.nextLine();
scanner.close();
System.out.println("Number of occurrences of \"" + str2 + "\" in \"" + str1 + "\":" + countSubstring(str1, str2));
}
static int countSubstring(String str1, String str2) {
// recursive function
if(str1.contains(str2)) {
return 1 + countSubstring(str1.replaceFirst(str2, ""), str2);
} else {
return 0;
}
}
}
/*
* Sample input/output:
* Enter first string:
* abcdabc
* Enter second string:
* abc
* Number of occurrences of "abc" in "abcdabc":2
*
* Time complexity: O(n)
*/