-
Notifications
You must be signed in to change notification settings - Fork 2
/
Count occurrences of a given word in a 2-d array.cc
123 lines (68 loc) · 2.46 KB
/
Count occurrences of a given word in a 2-d array.cc
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//{ Driver Code Starts
// Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
//class Solution{
// public:
//int findOccurrence(vector<vector<char> > &mat, string target){
class Solution
{
public:
int solve(int i, int j,vector<vector<char> > &mat, string target, int n,int idx ){
int count = 0;
// these are the boundary conditions
// see it like this we take starting position of string say idx and mat[i][j] is the starting pos of mat
// compare both of them, if we found a char we will store it and we will update that char with 0 to avoid looping
// now move forward and update idx.. if our idx reaches end of string means we have found out the word and increase the counter n return that
// else search in all four directions.
if(i>=0 && j>=0 && i<mat.size() && j<mat[0].size() && target[idx] == mat[i][j]){
char temp = target[idx];
mat[i][j] = 0;
idx += 1;
if(idx == n){
count = 1;
}
else{
count += solve(i+1,j,mat,target,n,idx);
count += solve(i-1,j,mat,target,n,idx);
count += solve(i,j+1,mat,target,n,idx);
count += solve(i,j-1,mat,target,n,idx);
}
// why we are back tracking here is to search all the possible counters of a word.. If we don't, the counter will always be one..
mat[i][j] = temp; // backtrack
}
return count;
}
int findOccurrence(vector<vector<char> > &mat, string target){
// this can be understood easily
int ans = 0;
int n = target.size();
for(int i=0; i<mat.size(); i++){
for(int j=0; j<mat[0].size(); j++){
ans += solve(i,j,mat,target,n,0);
}
}
return ans;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int R, C;
cin >> R >> C;
vector<vector<char> > mat(R);
for (int i = 0; i < R; i++) {
mat[i].resize(C);
for (int j = 0; j < C; j++) cin >> mat[i][j];
}
string target;
cin >> target;
Solution obj;
cout<<obj.findOccurrence(mat,target)<<endl;
}
}
// } Driver Code Ends