|
| 1 | +/* |
| 2 | + * @lc app=leetcode id=1268 lang=cpp |
| 3 | + * |
| 4 | + * [1268] Search Suggestions System |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/search-suggestions-system/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Medium (58.86%) |
| 10 | + * Likes: 178 |
| 11 | + * Dislikes: 62 |
| 12 | + * Total Accepted: 15.5K |
| 13 | + * Total Submissions: 26.2K |
| 14 | + * Testcase Example: '["mobile","mouse","moneypot","monitor","mousepad"]\r\n"mouse"\r' |
| 15 | + * |
| 16 | + * Given an array of strings products and a string searchWord. We want to |
| 17 | + * design a system that suggests at most three product names from products |
| 18 | + * after each character of searchWord is typed. Suggested products should have |
| 19 | + * common prefix with the searchWord. If there are more than three products |
| 20 | + * with a common prefix return the three lexicographically minimums products. |
| 21 | + * |
| 22 | + * Return list of lists of the suggested products after each character of |
| 23 | + * searchWord is typed. |
| 24 | + * |
| 25 | + * |
| 26 | + * Example 1: |
| 27 | + * |
| 28 | + * |
| 29 | + * Input: products = ["mobile","mouse","moneypot","monitor","mousepad"], |
| 30 | + * searchWord = "mouse" |
| 31 | + * Output: [ |
| 32 | + * ["mobile","moneypot","monitor"], |
| 33 | + * ["mobile","moneypot","monitor"], |
| 34 | + * ["mouse","mousepad"], |
| 35 | + * ["mouse","mousepad"], |
| 36 | + * ["mouse","mousepad"] |
| 37 | + * ] |
| 38 | + * Explanation: products sorted lexicographically = |
| 39 | + * ["mobile","moneypot","monitor","mouse","mousepad"] |
| 40 | + * After typing m and mo all products match and we show user |
| 41 | + * ["mobile","moneypot","monitor"] |
| 42 | + * After typing mou, mous and mouse the system suggests ["mouse","mousepad"] |
| 43 | + * |
| 44 | + * |
| 45 | + * Example 2: |
| 46 | + * |
| 47 | + * |
| 48 | + * Input: products = ["havana"], searchWord = "havana" |
| 49 | + * Output: |
| 50 | + * [["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]] |
| 51 | + * |
| 52 | + * |
| 53 | + * Example 3: |
| 54 | + * |
| 55 | + * |
| 56 | + * Input: products = ["bags","baggage","banner","box","cloths"], searchWord = |
| 57 | + * "bags" |
| 58 | + * Output: |
| 59 | + * [["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]] |
| 60 | + * |
| 61 | + * |
| 62 | + * Example 4: |
| 63 | + * |
| 64 | + * |
| 65 | + * Input: products = ["havana"], searchWord = "tatiana" |
| 66 | + * Output: [[],[],[],[],[],[],[]] |
| 67 | + * |
| 68 | + * |
| 69 | + * |
| 70 | + * Constraints: |
| 71 | + * |
| 72 | + * |
| 73 | + * 1 <= products.length <= 1000 |
| 74 | + * There are no repeated elements in products. |
| 75 | + * 1 <= Σ products[i].length <= 2 * 10^4 |
| 76 | + * All characters of products[i] are lower-case English letters. |
| 77 | + * 1 <= searchWord.length <= 1000 |
| 78 | + * All characters of searchWord are lower-case English letters. |
| 79 | + * |
| 80 | + * |
| 81 | + */ |
| 82 | + |
| 83 | +// @lc code=start |
| 84 | + |
| 85 | +class Solution { |
| 86 | +public: |
| 87 | + struct Node { |
| 88 | + bool isLast; |
| 89 | + map<char, Node*> children; |
| 90 | + Node(){ |
| 91 | + isLast = false; |
| 92 | + children.clear(); |
| 93 | + } |
| 94 | + }; |
| 95 | + void buildTree(Node* head, string& s, int i){ |
| 96 | + if((int)s.size() == i){ |
| 97 | + head->isLast = true; |
| 98 | + return; |
| 99 | + } |
| 100 | + if(head->children.find(s[i]) == head->children.end()) |
| 101 | + head->children[s[i]] = new Node(); |
| 102 | + buildTree(head->children[s[i]], s, i+1); |
| 103 | + } |
| 104 | + |
| 105 | + vector<vector<string>> ans; |
| 106 | + vector<string> tmpvec; |
| 107 | + string tmpst, prefix; |
| 108 | + |
| 109 | + void traverse(Node* head){ |
| 110 | + if((int)tmpvec.size() >= 3) |
| 111 | + return; |
| 112 | + |
| 113 | + if(head->isLast) |
| 114 | + tmpvec.push_back(tmpst); |
| 115 | + |
| 116 | + for(auto elem: head->children){ |
| 117 | + tmpst += elem.first; |
| 118 | + traverse(elem.second); |
| 119 | + tmpst.pop_back(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + void traverseHelper(Node* head){ |
| 124 | + tmpst = ""; |
| 125 | + tmpvec.clear(); |
| 126 | + if(head != NULL) |
| 127 | + traverse(head); |
| 128 | + vector<string> final; |
| 129 | + for(auto &e: tmpvec) |
| 130 | + final.push_back(prefix+e); |
| 131 | + ans.push_back(final); |
| 132 | + } |
| 133 | + |
| 134 | + void callme(Node* head, string& s, int i){ |
| 135 | + if((int)s.size() == i) return; |
| 136 | + prefix += s[i]; |
| 137 | + if(head == NULL || head->children.find(s[i]) == head->children.end()){ |
| 138 | + traverseHelper(NULL); |
| 139 | + callme(NULL, s, i+1); |
| 140 | + }else{ |
| 141 | + traverseHelper(head->children[s[i]]); |
| 142 | + callme(head->children[s[i]], s, i+1); |
| 143 | + } |
| 144 | + prefix.pop_back(); |
| 145 | + } |
| 146 | + |
| 147 | + vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) { |
| 148 | + Node* head = new Node(); |
| 149 | + for(string& prod: products) |
| 150 | + buildTree(head, prod, 0); |
| 151 | + prefix = ""; |
| 152 | + callme(head, searchWord, 0); |
| 153 | + |
| 154 | + return ans; |
| 155 | + } |
| 156 | +}; |
| 157 | +// @lc code=end |
0 commit comments