Skip to content

Commit f4b6095

Browse files
committed
Problem 168: Search a word in a grid
1 parent c07d9d8 commit f4b6095

File tree

2 files changed

+143
-2
lines changed

2 files changed

+143
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 167 |
9+
| Total Problems | 168 |
1010

1111
</center>
1212

@@ -197,12 +197,13 @@ Include contains single header implementation of data structures and some algori
197197
| Calculate total weight of Minimum Spanning Tree of a given graph ( sum of weights of edges which forms MST) using Prim's algorithm | [primsMST.cpp](graph_problems/primsMST.cpp)|
198198
| Print Minimum Spanning Tree( MST ) of a given graph using Kruskal's algorithm.| [kruskalMST.cpp](graph_problems/kruskalMST.cpp)|
199199
| Create a program to generate a Huffman encoding for each character as a table.|[huffman_encoding.cpp](greedy_problems/huffman_encoding.cpp)|
200+
| Search a given word in a 2D board containing letters. The word can be constructed by sequentially traversing adjacent horizontal or vertical cells. In a sequence to form word, letter on same position can not be used more than once. (Check top of file for examples.)|[grid_word_search.cpp](graph_problems/grid_word_search.cpp)|
200201

201202
### Greedy Problems
202203
| Problem | Solution |
203204
| :------------ | :----------: |
204205
| Given two integer arrays, A and B, each containing N integers. You are free to permute the order of the elements in the arrays. Is there an permutation A', B' possible of A and B, such that, A'<sub>i</sub>+B'<sub>i</sub> ≥ K for all i, where A'<sub>i</sub> denotes the i<sup>th</sup> element in the array A' and B'<sub>i</sub> denotes i<sup>th</sup> element in the array B'.| [two_arrays.cpp](greedy_problems/two_arrays.cpp)|
205-
|John is taking orders. The i<sup>th</sup> order is placed by the i<sup>th</sup> customer at t<sub>i</sub> time and it takes d<sub>i</sub> time to procees. What is the order in which the customers will get their orders? (see more details in solutions's comments)|[orders_order.cpp](greedy_problems/orders_order.cpp)|
206+
| John is taking orders. The i<sup>th</sup> order is placed by the i<sup>th</sup> customer at t<sub>i</sub> time and it takes d<sub>i</sub> time to procees. What is the order in which the customers will get their orders? (see more details in solutions's comments)|[orders_order.cpp](greedy_problems/orders_order.cpp)|
206207

207208

208209
### Leet code Problems

graph_problems/grid_word_search.cpp

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Search a given word in a 2D board containing letters.
3+
* The word can be constructed by sequentially traversing adjacent
4+
* horizontal or vertical cells. In a sequence to form word,
5+
* letter on same position can not be used more than once.
6+
*
7+
* Example:
8+
* Input:
9+
*
10+
* ['H' 'E' 'Y' 'A']
11+
* ['O' 'L' 'A' 'Y']
12+
* ['I' 'L' 'O' 'V']
13+
*
14+
* Word: HELLO
15+
* Output: Yes/true
16+
*
17+
* Word: YELL
18+
* Output: Yes/true
19+
*
20+
* Word: LOVE
21+
* Output: No/false
22+
*
23+
* Assumption: grid or word does not contain '$' char
24+
*
25+
* Approach: We can use Depth First Search with backtracking to solve this.
26+
* We can search the grid to match the grid the first letter of search word
27+
* in the grid, and then apply depth first search on the grid.
28+
* Finding appropriate next characters in the search word at each depth while
29+
* searching sequentially in the four directions.
30+
*
31+
* We need some way to mark a position in grid as already visited. I will use
32+
* '$' to mark the position as visited, and we will reset the letter at the
33+
* position back to original letter at the end of backtracking.
34+
*
35+
*/
36+
37+
#include <iostream>
38+
#include <vector>
39+
#include <iomanip>
40+
41+
bool depth_first_search(std::vector<std::vector<char>>& grid, const std::string& word,
42+
int r, int c, int index)
43+
{
44+
if (index == word.size()) {
45+
return true;
46+
}
47+
int rows = grid.size();
48+
int cols = grid[0].size();
49+
if (r < 0 || r >= rows || c < 0 || c >= cols) {
50+
return false;
51+
}
52+
53+
if (word[index] != grid[r][c]) {
54+
return false;
55+
}
56+
57+
char cur = grid[r][c];
58+
grid[r][c] = '$';
59+
bool result = false;
60+
61+
result = depth_first_search(grid, word, r - 1, c, index + 1) ||
62+
depth_first_search(grid, word, r + 1, c, index + 1) ||
63+
depth_first_search(grid, word, r, c - 1, index + 1) ||
64+
depth_first_search(grid, word, r, c + 1, index + 1);
65+
66+
grid[r][c] = cur;
67+
return result;
68+
}
69+
70+
71+
72+
bool grid_search(std::vector<std::vector<char>>& grid, const std::string& word)
73+
{
74+
int rows = grid.size();
75+
int cols = grid[0].size();
76+
for (int i = 0; i < rows; ++i) {
77+
for (int j = 0; j < cols; ++j) {
78+
if (word[0] == grid[i][j]) {
79+
if (depth_first_search(grid, word, i, j, 0)) {
80+
return true;
81+
}
82+
}
83+
}
84+
}
85+
return false;
86+
}
87+
88+
void print_grid(const std::vector<std::vector<char>>& grid)
89+
{
90+
for (auto vec : grid) {
91+
std::cout << "{ ";
92+
for (auto c : vec) {
93+
std::cout << c << " ";
94+
}
95+
std::cout << "}" << std::endl;
96+
}
97+
}
98+
99+
int main()
100+
{
101+
std::vector<std::vector<char>> grid{
102+
{'H', 'E', 'Y', 'A'},
103+
{'O', 'L', 'A', 'Y'},
104+
{'I', 'L', 'O', 'V'}
105+
};
106+
print_grid(grid);
107+
std::cout << std::boolalpha;
108+
std::cout << "Does word 'HELLO' exist in grid? ";
109+
bool result = grid_search(grid, "HELLO");
110+
std::cout << result << std::endl;
111+
112+
std::cout << "Does word 'HELL' exist in grid? ";
113+
result = grid_search(grid, "HELL");
114+
std::cout << result << std::endl;
115+
116+
std::cout << "Does word 'LOVE' exist in grid? ";
117+
result = grid_search(grid, "LOVE");
118+
std::cout << result << std::endl;
119+
120+
std::vector<std::vector<char>> grid2 {
121+
{'A','B','C','E'},
122+
{'S','F','C','S'},
123+
{'A','D','E','E'}
124+
};
125+
126+
print_grid(grid2);
127+
std::cout << "Does word 'SEE' exist in grid? ";
128+
result = grid_search(grid2, "SEE");
129+
std::cout << result << std::endl;
130+
131+
std::cout << "Does word 'ABCCSE' exist in grid? ";
132+
result = grid_search(grid2, "ABCCSE");
133+
std::cout << result << std::endl;
134+
135+
std::cout << "Does word 'ABDE' exist in grid? ";
136+
result = grid_search(grid2, "ABDE");
137+
std::cout << result << std::endl;
138+
139+
return 0;
140+
}

0 commit comments

Comments
 (0)