Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions week04/implement_trie_prefix_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Time complexity: Around O(N * M) where N is Number of strings and M is average length of the strings.
// Space complexity: Around O(N * M) or less.

class TrieNode{
public:
bool isWord;
TrieNode* next[26];
TrieNode(){
isWord = false;
for(int i = 0; i < 26; i++){
next[i] = NULL;
}
}
};

class Trie{
public:
TrieNode root;
Trie(){
root = new TrieNode();
}

void insert(string word){
TrieNode currNode = root;
for(auto c: word){
int order = c - 'a';
if(currNode->next[order] == NULL){
currNode->next[order] = new TrieNode();
}
currNode = currNode->next[order];
}
currNode->isWord = true;
}

bool search(string word, bool isPrefixSearch = false){
TrieNode currNode = root;
for(auto c: word){
int order = c - 'a';
if(currNode->next[order] == NULL){
return false;
}
currNode = currNode->next[order];
}
return currNode && (currNode->isWord || isPrefixSearch);
}

bool startsWith(string prefix){
return search(prefix, true);
}
};
49 changes: 49 additions & 0 deletions week04/search_suggestions_system.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class trieNode{
public:
string word;
trieNode* next[26];
trieNode(){
word = "";
for(int i = 0; i < 26; i++){
next[i] = NULL;
}
}
};

trieNode* generateTrie(vector<string>& products){
trieNode* root = new trieNode();
for(auto &wd: products){
trieNode* curr = root;
for(auto c: wd){
int order = c - 'a';
if(curr->next[order] == NULL){
curr->next[order] = new trieNode();
}
curr = cur->next[order];
}
curr->word = wd;
}
return root;
}

void dfs(trieNode* u, vector<string>& words){
if(u == NULL || words.size() == 3) return;
if(u->word.size() > 0 && words.size() < 3){
words.push_back(u->word);
}
for(int i = 0; i < 26; i++){
dfs(u->next[i], words);
}
}

vector<vector<string>> suggestedProducts(vector<string>& products, string searchWword){
trieNode* root = generateTrie(products);
vector<vector<string>> suggestions;
for(auto c: searchWord){
root = root && root->next[c-'a'] ? root->next[c-'a'] : NULL;
vector<string> words;
dfs(root, words);
suggestions.push_back(words);
}
return suggestions;
}
66 changes: 66 additions & 0 deletions week04/word_search_ii.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Time complexity: O(N * M) * O(4 * 3 ^ (length of word)), N = number of rows, M = number of columns.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Time complexity: O(N * M) * O(4 * 3 ^ (length of word)), N = number of rows, M = number of columns.
// Time complexity: O(N * M * 4 * 3 ^ (length of word)), N = number of rows, M = number of columns.

class trieNode{
public:
string word;
trieNode* next[26];
trieNode(){
word = "";
for(int i = 0; i < 26; i++){
next[i] = NULL;
}
}
};

vector<int> dir{-1, 0, 1, 0, -1};

trieNode* generateTrie(vector<string> &words){
trieNode* root = new trieNode();
for(auto &wd: words){
trieNode* currNode = root;
for(auto &c: wd){
int order = c - 'a';
if(currNode->next[order] == NULL){
currNode->next[order] = new trieNode();
}
currNode = currNode->next[order];
}
currNode->word = wd;
}
return root;
}

void dfs(int x, int y, vector<vector<char>> &board, trieNode* currNode, vector<string> &wordsOnBoard){
char ch = board[x][y];
if(ch == '#' || (currNode && currNode->next[ch-'a'] == NULL)) return;
currNode = currNode->next[ch-'a'];

if(currNode && currNode->word.size() > 0){
wordsOnBoard.push_back(currNode->word);
currNode->word.clear();
}

board[x][y] = '#';

for(int i = 0; i < 4; i++){
int X = x + dir[i];
int Y = y + dir[i+1];
if(X < 0 || Y < 0 || X >= board.size() || Y >= board[0].size() || board[X][Y] == '#') continue;
dfs(X, Y, board, currNode, wordsOnBoard);
}

board[x][y] = ch;
}

vector<string> findWords(vector<vector<char>> &board, vector<string> &words){
trieNode* root = generateTrie(words);
vector<string> wordsOnBoard;
int n = board.size();
int m = board[0].size();

for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
dfs(i, j, board, root, wordsOnBoard);
}
}
return wordsOnBoard;
}