FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaProgramming/Recursion/Occurance.java at main · PrityKumari14/JavaProgramming · GitHub
PrityKumari14
/
JavaProgramming
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaProgramming
/
Recursion
/
Occurance.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
30 lines (28 loc) · 1.04 KB
Breadcrumbs
JavaProgramming
/
Recursion
/
Occurance.java
Copy path
File metadata and controls
30 lines (28 loc) · 1.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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'
);
}
}
Back
|
FazBrowse Home
|
New Git URL