diff --git a/Trees/HotelReviews.cpp b/Trees/HotelReviews.cpp new file mode 100644 index 0000000..2820e5e --- /dev/null +++ b/Trees/HotelReviews.cpp @@ -0,0 +1,107 @@ +// https://www.interviewbit.com/problems/hotel-reviews/ + +struct TrieNode{ + unordered_map children; + bool is_word; + TrieNode(bool x) : is_word(x) {} +}; + +struct ResNode{ + int index; + int val; +}; + +bool myfunction (ResNode i,ResNode j) { + bool ret = 0; + if(i.val>j.val){ + ret=1; + } + else if(i.val==j.val){ + if(i.index::iterator it = (current->children).find(good_words[i]); + if(it != (current->children).end()){ + current = it->second; + } + else{ + TrieNode* B = new TrieNode(0); + (current->children)[good_words[i]] = B; + current=B; + } + if(i==good_words.length()-1 || good_words[i+1]=='_'){ + current->is_word = 1; + } + } + } +} + +int solveReview(TrieNode* A, string review){ + int result = 0; + review = review+"_"; + vector arr; + int start = 0; + for(int i=0; i::iterator it = (current->children).find(arr[i][j]); + if(it != (current->children).end()){ + current = it->second; + } + else{ + break; + } + if(j==arr[i].length()-1){ + if(current->is_word == 1){ + result++; + } + } + } + } + return result; +} + +vector Solution::solve(string good_words, vector &reviews) { + // Do not write main() function. + // Do not read input, instead use the arguments to the function. + // Do not print the output, instead return values as specified + // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details + + vector result; + vector temp_res; + TrieNode* A = new TrieNode(0); + construct_trie(good_words, A); + + for(int i=0; i