-
Notifications
You must be signed in to change notification settings - Fork 66
/
occurances.java
54 lines (33 loc) · 1.21 KB
/
occurances.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//username DishaKhanapurkar
class Solution
{
public int findOccurrence(char mat[][], String target)
{
int n=mat.length;
int m=mat[0].length;
int count=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++){
if(mat[i][j]==target.charAt(0)){
count+=dfs(mat,target,i,j,0);
}
}
}
return count;
}
public int dfs(char[][]mat,String target,int i,int j,int ind)
{
if(i>=mat.length||j>=mat[0].length||i<0||j<0||mat[i][j]=='*'||target.charAt(ind)!=mat[i][j])
return 0;
if(ind==target.length()-1)
return 1;
char temp=mat[i][j];
mat[i][j]='*';
int u=dfs(mat,target,i+1,j,ind+1);
int d=dfs(mat,target,i-1,j,ind+1);
int r=dfs(mat,target,i,j+1,ind+1);
int l=dfs(mat,target,i,j-1,ind+1);
mat[i][j]=temp;
return u+d+r+l;
}}