forked from bindubritto/tjip-leetcode-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Submission week four #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nazmulwanted
wants to merge
4
commits into
master
Choose a base branch
from
week_four
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
950e47d
Problem 1: Implement trie prefix tree. Done.
nazmulwanted 754c8d2
Problem 5: Search suggestions system. Done.
nazmulwanted fde27fc
Problem 3: Word Search II. Done.
nazmulwanted f590765
Problem 3: Word Search II. Added comments. Done.
nazmulwanted File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.