Skip to content

Commit

Permalink
Added solutions for: 211. Design Add and Search Words Data Structure: h…
Browse files Browse the repository at this point in the history
  • Loading branch information
asesh committed Nov 17, 2024
1 parent e7ac150 commit cae27b1
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 24 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "452"
endingLineNumber = "452"
startingLineNumber = "454"
endingLineNumber = "454"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -30,8 +30,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "418"
endingLineNumber = "418"
startingLineNumber = "420"
endingLineNumber = "420"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -46,8 +46,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "343"
endingLineNumber = "343"
startingLineNumber = "345"
endingLineNumber = "345"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -62,8 +62,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "311"
endingLineNumber = "311"
startingLineNumber = "313"
endingLineNumber = "313"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -110,8 +110,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "273"
endingLineNumber = "273"
startingLineNumber = "275"
endingLineNumber = "275"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -126,8 +126,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "236"
endingLineNumber = "236"
startingLineNumber = "238"
endingLineNumber = "238"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -142,8 +142,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "195"
endingLineNumber = "195"
startingLineNumber = "197"
endingLineNumber = "197"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -174,8 +174,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "184"
endingLineNumber = "184"
startingLineNumber = "186"
endingLineNumber = "186"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -190,8 +190,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "170"
endingLineNumber = "170"
startingLineNumber = "172"
endingLineNumber = "172"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down
4 changes: 3 additions & 1 deletion algorithm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#include "trie.hpp"

int main(int argc, const char* argv[]) {
invoke_trie_methods();
invoke_longest_common_prefx();

// invoke_trie_methods();

// invoke_gas_station();

Expand Down
26 changes: 26 additions & 0 deletions algorithm/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,3 +771,29 @@ void invoke_valid_sudoku() {
};
std::cout<<"The validity of Sudoku: "<< std::boolalpha << valid_sudoku(input);
}

/*
Input: ["flower", "flow", "flight"], Output: "fl"
Process:
sort: flight, flow, flower
compare flight[i...] with flower[i...] until and unless they are equal
*/
std::string longest_common_prefx(std::vector<std::string>& input) {
std::string output;
std::sort(input.begin(), input.end());

std::string first = input[0], last = input[input.size() - 1];
for(int index = 0; index < std::min(first.size(), last.size()); ++index) {
if(first[index] != last[index]) {
return output;
}

output += first[index];
}

return output;
}
void invoke_longest_common_prefx() {
std::vector<std::string> input = {"flower", "flow", "flight"}; // {"dog","racecar","car"}
std::cout<<"The longest common prefix is: "<<longest_common_prefx(input);
}
5 changes: 5 additions & 0 deletions algorithm/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ void invoke_group_anagrams();
*/
void invoke_valid_sudoku();

/*
14. Longest Common Prefix: https://leetcode.com/problems/longest-common-prefix
*/
void invoke_longest_common_prefx();

#endif /* string_hpp */
21 changes: 16 additions & 5 deletions algorithm/trie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ Trie visualization:
(*)
/ \
(a) (h)
/ \
(s) (i)
\
(m)
/ / \
(s) (e) (i)
/ \
(r) (m)
*/
void CTrie::insert(std::string word) {
CTrie* current_node = this;
Expand All @@ -36,7 +36,16 @@ void CTrie::insert(std::string word) {

bool CTrie::search(std::string word) {
CTrie* current_node = this;
for(auto& character: word) {
for(int index = 0; index < word.size(); ++index) {//for(auto& character: word) {
auto character = word[index];
if(character == '.') {
for(auto& child_node: current_node->m_child) {
if(child_node && child_node->search(word.substr(index + 1))) {
return true;
}
}
return false;
}
if(!current_node->m_child[character - 'a']) {
return false;
}
Expand All @@ -63,10 +72,12 @@ void invoke_trie_methods() {
trie.insert("hello");
auto search_one = trie.search("hello");
auto search_two = trie.search("foo");
auto search_three = trie.search("..llo");
auto starts_with = trie.starts_with("hel");
std::cout<<"Trie (prefix tree): "
<<std::boolalpha
<<"search result for 'hello': "<<search_one
<<", search result for 'foo': "<<search_two
<<", search result for '..llo': "<<search_three
<<", starts with 'hel': "<<starts_with<<std::endl;
}
1 change: 1 addition & 0 deletions algorithm/trie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class CTrie {

/*
208. Implement Trie (Prefix Tree): https://leetcode.com/problems/implement-trie-prefix-tree
211. Design Add and Search Words Data Structure: https://leetcode.com/problems/design-add-and-search-words-data-structure
*/
void invoke_trie_methods();

Expand Down

0 comments on commit cae27b1

Please sign in to comment.