Skip to content

Instantly share code, notes, and snippets.

@pradeep1288
Created March 7, 2016 21:43
Show Gist options
  • Save pradeep1288/4dee3a6ca4cd527e4bb6 to your computer and use it in GitHub Desktop.
Save pradeep1288/4dee3a6ca4cd527e4bb6 to your computer and use it in GitHub Desktop.
/*
Given a 2D board and a list of words from the dictionary, find all words in the board.
Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
For example,
Given words = ["oath","pea","eat","rain"] and board =
[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]
Return ["eat","oath"].
Note:
You may assume that all inputs are consist of lowercase letters a-z.
*/
class TrieNode {
public:
TrieNode() {
is_string = false;
}
public:
bool is_string;
unordered_map<char, TrieNode*> leaves;
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
void insert(string word) {
TrieNode* cur = root;
for (string::iterator it = word.begin();it<word.end(); it++) {
if (!cur->leaves.count(*it)) {
cur->leaves[*it] = new TrieNode();
}
cur = cur->leaves[*it];
}
cur->is_string = true;
}
public:
TrieNode* root;
};
class Solution {
public:
/**
* @param board: A list of lists of character
* @param words: A list of string
* @return: A list of string
*/
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
string cur;
vector<vector<bool>> visited(board.size(), vector<bool>(board[0].size(), false));
Trie trie;
for (int i =0; i<words.size();i++) {
trie.insert(words[i]);
}
for (int i =0; i<board.size();i++) {
for (int j =0; j<board[0].size();j++) {
findWordsDFS(board,visited,i,j,trie.root,cur);
}
}
return vector<string>(result.begin(), result.end());
}
void findWordsDFS(vector<vector<char>>& board, vector<vector<bool>>& visited, int r, int c, TrieNode* curNode, string currentStr) {
if (!curNode || r<0 || r >= board.size() || c<0 || c>=board[0].size())
return;
if (!curNode->leaves[board[r][c]] || visited[r][c])
return;
TrieNode* next = curNode->leaves[board[r][c]];
currentStr.push_back(board[r][c]);
if (next->is_string)
result.insert(currentStr);
visited[r][c] = true;
vector<pair<int,int>> direction = {{0,1}, {0,-1},{1,0},{-1,0}};
for (vector<pair<int,int>>::iterator it = direction.begin();it<direction.end();it++) {
findWordsDFS(board,visited,r+it->first,c+it->second,next,currentStr);
}
visited[r][c] = false;
}
private:
unordered_set<string> result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment