Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c0ace54

Browse files
committed
Problem : 186, search word in a grid from a dictionary
1 parent 0e8c0ab commit c0ace54

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-1
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 186 |
9+
| Total Problems | 187 |
1010

1111
</center>
1212

@@ -219,6 +219,7 @@ Include contains single header implementation of data structures and some algori
219219
| :------------ | :----------: |
220220
| You are given a digit string (e.g "1234", "567" etc), provide all possible letter combinations we could generate from this digit string, based on the mapping we see on the telphone/mobile dialpad. If you have typed SMS in old style phones, you would know. For e.g. "1" is mapped to "abc", 2 is mapped to "def". You can refer to the <a href="http://techotv.com/wp-content/uploads/2013/03/Lumia-620-dial-pad-screen-1.jpg" target="_blank">image.</a>. <ul><li> Example: "34" will give output: {"dg","dh","di","eg","eh","ei","fg","fh","fi"} </li></ul> Note that order does not matter in result set.|[dialpad_combinations.cpp](backtracking_problems/dialpad_combinations.cpp)|
221221
| Implement wildcard pattern maching with support for '?' & '*'. <ul><li>'?' Matches any single character.</li></ul><ul><li>'*' Matches any sequence of character.</li></ul>. Checkout examples in file for more details.|[wild_card_matching.cpp](backtracking_problems/wild_card_matching.cpp)|
222+
| Given a 2D board and list of words from a dictionary, find all the possible words on board fromt the list. (Check example in the solution)| [word_search.cpp](backtracking_problems/word_search.cpp)|
222223

223224

224225

‎backtracking_problems/word_search.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Given a 2D board and list of words from a dictionary,
3+
* find all the possible words on board fromt the list.
4+
* Example:
5+
* words = ["oath","pea","eat","rain"]
6+
* board =
7+
* [
8+
* ['o','a','a','n'],
9+
* ['e','t','a','e'],
10+
* ['i','h','k','r'],
11+
* ['i','f','l','v']
12+
* ]
13+
*
14+
* Output: ["eat", "oath"]
15+
*
16+
* Source : LeetCode Problem 212
17+
*
18+
* Approach:
19+
*
20+
* We can use backtracking(dfs) to traverse from each character on board
21+
* to all four direction. In order to do a fast searching, we can use
22+
* a trie from words in dictionary, as soon as we find a word from dictionary
23+
* in trie, we remove it from trie, so we don't have duplicates.
24+
* Also, we can use the board to mark '#' as visited character to
25+
* achieve backtracking.
26+
*/
27+
28+
#include <vector>
29+
#include <iostream>
30+
31+
struct TrieNode {
32+
std::string word;
33+
std::vector<TrieNode*> children;
34+
TrieNode():
35+
word{""}, children{std::vector<TrieNode*>(26, nullptr)}{}
36+
};
37+
38+
TrieNode* build_trie(const std::vector<std::string>& dictionary)
39+
{
40+
TrieNode* root = new TrieNode();
41+
for (auto word: dictionary) {
42+
TrieNode* curr = root;
43+
for (auto c: word) {
44+
int index = c - 'a';
45+
if (curr->children[index] == nullptr) {
46+
curr->children[index] = new TrieNode();
47+
}
48+
curr = curr->children[index];
49+
}
50+
curr->word = word;
51+
}
52+
return root;
53+
}
54+
55+
void backtrack(std::vector<std::vector<char>>& board,
56+
TrieNode* root, int i, int j, std::vector<std::string>& result)
57+
{
58+
char c = board[i][j];
59+
if (c == '#' || root == nullptr) {
60+
return;
61+
}
62+
int index = c - 'a';
63+
TrieNode* curr = root->children[index];
64+
if (curr == nullptr) {
65+
return;
66+
}
67+
68+
if (curr->word != "") {
69+
result.push_back(curr->word);
70+
// Reset to avoid duplicates.
71+
curr->word = "";
72+
}
73+
74+
board[i][j] = '#';
75+
if (i > 0) {
76+
backtrack(board, curr, i - 1, j, result);
77+
}
78+
if (j > 0) {
79+
backtrack(board, curr, i, j - 1, result);
80+
}
81+
if (i < board.size() - 1) {
82+
backtrack(board, curr, i + 1, j, result);
83+
}
84+
if (j < board[0].size() - 1) {
85+
backtrack(board, curr, i, j + 1, result);
86+
}
87+
board[i][j] = c;
88+
}
89+
90+
std::vector<std::string> find_words(std::vector<std::vector<char>>& board,
91+
std::vector<std::string>& dictionary)
92+
{
93+
std::vector<std::string> result;
94+
for (int i = 0; i < board.size(); ++i) {
95+
for (int j = 0; j < board[0].size(); ++j) {
96+
TrieNode* root = build_trie(dictionary);
97+
backtrack(board, root, i, j, result);
98+
}
99+
}
100+
return result;
101+
}
102+
103+
template <typename T>
104+
void print_vector(const std::vector<T>& vec)
105+
{
106+
std::cout << "{ ";
107+
for (auto t : vec) {
108+
std::cout << t << " ";
109+
}
110+
std::cout << " }" << std::endl;
111+
}
112+
113+
void print_board(const std::vector<std::vector<char>>& board)
114+
{
115+
for (auto v: board) {
116+
print_vector(v);
117+
}
118+
}
119+
120+
121+
int main()
122+
{
123+
std::vector<std::vector<char>> board = {
124+
{'o','a','a','n'},
125+
{'e','t','a','e'},
126+
{'i','h','k','r'},
127+
{'i','f','l','v'}
128+
};
129+
130+
std::vector<std::string> dictionary = {
131+
"oath","pea","eat","rain"
132+
};
133+
134+
std::vector<std::string> result = find_words(board, dictionary);
135+
std::cout << "Board:" << std::endl;
136+
print_board(board);
137+
std::cout << "Dictionary:" << std::endl;
138+
print_vector(dictionary);
139+
std::cout << "Result:" << std::endl;
140+
print_vector(result);
141+
return 0;
142+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /