-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOccurance.java
More file actions
30 lines (28 loc) · 1.04 KB
/
Occurance.java
File metadata and controls
30 lines (28 loc) · 1.04 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
//Q3. Find the occurrence of the first and last occurrence of an element using recursion.
// that means koi element string me first time kab aaya aur last time kab aaya
package Recursion;
public class Occurance {
// hum apna first and last variables ko static bnayenge kyonki isko bar bar hum recursion function ke undar call nhi karna chahte
public static int first = -1;
public static int last = -1;
public static void findOccurance(String str, int idx, char element){
if(idx == str.length()){
System.out.println("First Occurance in index : "+first);
System.out.println("Last Occurance in index : "+last);
return;
}
char currChar = str.charAt(idx);
if(currChar == element){
if(first == -1){
first = idx;
}else{
last = idx;
}
}
findOccurance(str, idx+1, element);
}
public static void main(String[] args) {
String str = "abaacdaefaah";
findOccurance(str, 0, 'a');
}
}