-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPracitceQs4.java
More file actions
33 lines (25 loc) · 1.34 KB
/
PracitceQs4.java
File metadata and controls
33 lines (25 loc) · 1.34 KB
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
public class PracitceQs4 {
public static int solution(String s,int start,int end){
// Base Condition: If the starting index exceds the ending index.
if(start>end){
return 0;
}
// Initialise count current recursive call
int count = 0;
// Check if the substring from start to end is valid (i.e ,starts and ends with the same character)
if(s.charAt(start)==(s.charAt(end))){
count=1;
}
// Recursive call for smaller substring by moving the start index to the end index
return count+ solution(s, start+1, end)
+ solution(s, start,end-1)
- solution(s, start+1,end-1);
// Move start forward:solution(s,start+1,end): This call executes the character at the start position and looks for substring starting from start+1 to end.
// Move end backward: solution(s,start,end-1): This call executes the character at the end position and looks for substring starting from start to end-1;
// Exclude both start and end : solution(s,start+1,end-1):This call excludes both start and end characters and looks for substring starting from start+1 to end-1.
}
public static void main(String[] args) {
String S1 = "abcab";
System.out.println(solution(S1, 0, S1.length()-1));
}
}